在HandleUnauthorizedRequest

时间:2017-05-02 10:17:54

标签: asp.net-mvc authentication

我在MVC工作。我收到了错误

  

err_too_many_redirects

in

  

HandleUnauthorizedRequest

public class AuthCheckService : AuthorizeAttribute
{
    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
        {
            //Ajax request doesn't return to login page, it just returns 403 error.
            filterContext.HttpContext.Response.Clear();
            filterContext.HttpContext.Response.StatusCode = 501;
            throw new ModelStateException("Your Session has expired- Please login again!");
        }
        else
        {
            base.HandleUnauthorizedRequest(filterContext);
            //filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { action = "Login", controller = "Account" }));
        }

    }
}

请告诉我哪里出错了?

1 个答案:

答案 0 :(得分:0)

我在网络配置文件中测试时遇到了一个愚蠢的错误:

 <sessionState timeout="2" cookieName="OnshoreMaintSSID" regenerateExpiredSessionId="true" />
<authentication mode="Forms">
  <forms loginUrl="Account/Login" timeout="1" name=".Onshore" requireSSL="false" />
</authentication>
<pages>

如上所示,sessionstate超时大于表单身份验证超时。这导致了这个问题。

<强>解决方案:

 <sessionState timeout="1" cookieName="OnshoreMaintSSID" regenerateExpiredSessionId="true" />
<authentication mode="Forms">
  <forms loginUrl="Account/Login" timeout="2" name=".Onshore" requireSSL="false" />
</authentication>
<pages>

希望这会对其他人有所帮助。