2016-06-14 5 views
0

mvc 5アプリケーションでセッションタイムアウト後にホームページにリダイレクトする必要があります。送信ボタンをクリックした後MVCアクションフィルタでセッションタイムアウト後にリダイレクト

[SessionExpire] 
public ActionResult Submit() 
{ 
    var urlReferrer = this.Request.UrlReferrer; 
    if (urlReferrer != null) 
    { 
    return this.Redirect(urlReferrer.ToString()); 
    } 
    else 
    { 
     return View(); 
    } 
} 

、ページは以下などのJavaScriptコードを使用して別のページにリダイレクトされています、私はコードの下に持っているコントローラで

public class SessionExpireAttribute : ActionFilterAttribute 
    { 

     public override void OnActionExecuting(ActionExecutingContext filterContext) 
     { 

      var currentEnvironment = EnvironmentUtility.GetSession().Get<Environment>(); 
      if (currentEnvironment == null) 
      { 
       filterContext.Result = new RedirectResult("~/Home/Index"); 
      } 
      base.OnActionExecuting(filterContext); 
     } 

    } 

:私は、次のコードを使用しています:

function commitButtonClickedHandler() 
{ 
     window.location.href = "@Url.Content("~/Working/NextPage")"; 
} 

そしてNEXTPAGEは、以下のようなものです:

public ActionResult NextPage() 
     { 
      return this.View(); 
     } 

私の要件はセッションのタイムアウト後です。送信ボタンをクリックすると、セッションの有効期限を確認する必要があります。期限が切れている場合は、SessionExpireAttributeのようにNextPageにリダイレクトする必要があります。それ以外の場合はプロセスを続行する必要がありますが、問題はセッションのタイムアウト後もページがまだNextPage()アクションにリダイレクトされています。親切に私はこれを修正するために手伝ってください。 ありがとうございます。

+0

あなたはどのような認証を使用していますか?フォーム認証? –

+0

私はWindows認証を使用しています。 –

答えて

0

あなたは以下のようにいくつかの変更を行うOnActionExecutingをオーバーライドすることでそれを行うことができます:

public class SessionExpireAttribute : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    {    
      if (Session["SomeSession"] == null)      
      { 
       //ClearAllSessions 
       GoToLoginPage(filterContext, "You Session has been expired"); 
       return; 
      }   
    } 

    private static void GoToLoginPage(ActionExecutingContext filterContext, string message) 
    { 
     try 
     {   


      filterContext.Result = new RedirectToRouteResult(
       new RouteValueDictionary 
      { 
       { "controller", "controllerName" }, 
       { "action", "actionName" } , 
       { "Area","" }, 
       { "UnauthLogin",message }, 

      }); 
      } 
     } 
     catch 
     { 
      filterContext.Result = new RedirectToRouteResult(
        new RouteValueDictionary 
      { 
       { "controller", "controllerName" }, 
       { "action", "actionName" } , 
       { "Area","" }, 
       { "UnauthLogin",message }, 

      }); 
     } 
    } 
} 
+0

'filterContext.Result'プロパティを設定するtry/catchの必要はありません。アクションフィルタのコンテキスト内では実行されません。 – NightOwl888

+0

あなたは欠けてしまったbase.OnActionExecuting(filterContext);アクションフィルタからセッションにアクセスすることはできません。 –

関連する問題