1

asp.net MVCでのページ認証処理を改善しようとしていたので、このガイド(http://markfreedman.com/index.php/2012/02/28/handling-session-and-authentication-timeouts- )を読んでいました。 in-asp-net-mvc /)そして私は彼の例を試しました:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class SessionExpireFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        HttpContext ctx = HttpContext.Current;

        // If the browser session or authentication session has expired...
        if (ctx.Session["UserName"] == null || !filterContext.HttpContext.Request.IsAuthenticated)
        {
            if (filterContext.HttpContext.Request.IsAjaxRequest())
            {
                // For AJAX requests, we're overriding the returned JSON result with a simple string,
                // indicating to the calling JavaScript code that a redirect should be performed.
                filterContext.Result = new JsonResult { Data = "_Logon_" };
            }
            else
            {
                // For round-trip posts, we're forcing a redirect to Home/TimeoutRedirect/, which
                // simply displays a temporary 5 second notification that they have timed out, and
                // will, in turn, redirect to the logon page.
                filterContext.Result = new RedirectToRouteResult(
                    new RouteValueDictionary {
                        { "Controller", "Home" },
                        { "Action", "Index" }
                });
            }
        }

        base.OnActionExecuting(filterContext);
    }
}

しかし、私のリンクの多くが次のようなAjax.ActionLinksであることに気づきました。

<ul>@Ajax.ActionLink("Employees", "Index", "Employee", new AjaxOptions { UpdateTargetId = "employee_detail" })</ul>

これを行うと、認証の有効期限が切れた後にログインビューが正しく返されますが、ページ全体をリロードするのではなく、ページdiv内に返されます。それをビューに設定することは可能ですか?「div内にいる場合は、ページ全体をリロードしますか?」

ありがとう。

4

1 に答える 1

1

Ajax.ActionLinkのドキュメントから判断すると、200以外のステータスコードが返された場合にOnFailureが呼び出されるようです。属性には、次のようなステータスコードを必ず追加してください。

filterContext.Result = new JsonResult { Data = "_Logon_" };
filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;

それがそこにあると、OnFailureスクリプト名をActionLinkに追加できます。

@Ajax.ActionLink("Employees", "Index", "Employee", 
    new AjaxOptions { 
      UpdateTargetId = "employee_detail", 
      OnFailure="myRedirectFunctionName()" 
     })
于 2012-05-18T21:13:51.543 に答える