什么在会话超时后设置System.Security.Principal.Identity.IsAuthenticated?

时间:2012-05-10 15:08:53

标签: c# asp.net .net asp.net-mvc-3

我目前正在添加一个动作过滤器来处理我们网站中的会话超时:

[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标志在超时后为false,但是当它在动作过滤器中被击中时仍然是真的。 (我知道会话已经超时了,因为我在Global.asax的Session_End上设置了一个断点,这首先被击中了。)

我们网站的身份验证由公司标准的“单点登录”dll处理,因此我猜这是设置单独的身份验证超时,这听起来有可能吗?

感谢任何帮助。

3 个答案:

答案 0 :(得分:2)

我想你想用HttpContext.User.Identity替换Thread.CurrentPrincipal.Identity.IsAuthenticated

我认为你使用Thread.CurrentPrincipal做的是询问服务器上主动服务Web应用程序的用户是否经过身份验证。您要做的是询问用户是否经过身份验证。

答案 1 :(得分:1)

会话和身份验证cookie是不同的东西。您可以拥有仍经过身份验证但会话已过期的用户。看看这篇文章:asp.net cookies, authentication and session timeouts

答案 2 :(得分:0)

可能是一个警察,但在与业务中的其他团队协商后,我将遵循使用公司“单点登录”dll的其他网站的模型,并使用Session.KeepAlive方法,所以我'我不需要这个动作过滤器。

相关问题