2012-05-10 258 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仍然通過驗證。 –