2012-05-10 208 views
0

我目前增加一个动作过滤器来处理会话超时在我们的网站:会话超时后,什么设置System.Security.Principal.Identity.IsAuthenticated?

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)] 
public class SsoExpireFilterAttribute : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     if(!(filterContext.Controller.GetType() == typeof(HomeController) 
      && filterContext.ActionDescriptor.ActionName == MVC.Home.ActionNames.Index)) 
     { 
      if(filterContext.ActionDescriptor.ActionName != MVC.Home.ActionNames.TimeoutRedirect.ToLower()) 
      { 
       if (!Thread.CurrentPrincipal.Identity.IsAuthenticated) 
       { 
        if (filterContext.HttpContext.Request.IsAjaxRequest()) 
         filterContext.Result = new JsonResult { Data = "_Logon_" }; 
        else 
         filterContext.Result = new RedirectToRouteResult(
          new RouteValueDictionary 
         { 
          {"Controller", "Home"}, 
          {"Action", "TimeoutRedirect"} 
         }); 
       } 
      } 
     } 

     base.OnActionExecuting(filterContext); 
    } 
} 

我期待在Principal.Identity的IsAuthenticated标志是假的以下超时,但它是真正的剩余,当它在被击中动作过滤器。 (我知道会话已经超时了,因为我在Global.asax的Session_End中放了一个断点,这首先被触发)。

我们网站的身份验证是由公司标准的“单一登录”DLL处理的,所以我猜测这是设置一个单独的身份验证超时,这听起来可能吗?

任何帮助表示赞赏。

+0

我可能是错的,但我认为这篇文章可能会派上用场 http://www.hanselman.com/blog/SystemThreadingThreadCurrentPrincipalVsSystemWebHttpContextCurrentUserOrWhyFormsAuthenticationCanBeSubtle.aspx 或第 http://stackoverflow.com/questions/6810808/thread -currentprincipal-identity-vs-httpcontext-user-identity –

回答

0

可能有点警察出局,但经过与业务的其他团队咨询后,我将遵循使用企业“单一登录”DLL的其他网站的模型,并使用Session.KeepAlive方法,所以我不需要这个动作过滤器。

2

我想你想与HttpContext.User.Identity更换Thread.CurrentPrincipal.Identity.IsAuthenticated

我觉得跟你Thread.CurrentPrincipal中做什么,是询问用户当前正在服务的Web应用程序的服务器通过身份验证。你想要做的是询问用户是否被认证。

+0

感谢你们,我用HttpContext.User.Identity取代了Thread.CurrentPrincipal,但得到了相同的结果。我认为这是因为MajoB认为会话已超时,但身份验证Cookie仍在进行身份验证。 +1,因为我肯定从你的答案和Hansleman博客文章的链接中学到了一些东西 –

1

会话和身份验证Cookie是不同的事情。您可以让用户仍然通过身份验证,但会话已过期。看看这个职位:asp.net cookies, authentication and session timeouts

+0

谢谢,你是对的,因为我可以看到会话已经超时,但Principal.Identity仍然通过验证。 –