【问题标题】:What sets System.Security.Principal.Identity.IsAuthenticated after Session timeout?会话超时后 System.Security.Principal.Identity.IsAuthenticated 的设置是什么?
【发布时间】:2012-05-10 15:08:53
【问题描述】:

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

[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 处理,所以我猜这是在设置单独的身份验证超时,这听起来可能吗?

感谢任何帮助。

【问题讨论】:

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


【解决方案1】:

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

我认为您对 Thread.CurrentPrincipal 所做的事情是询问正在为您的服务器上的 Web 应用程序提供服务的用户是否经过身份验证。您要做的是询问用户是否经过身份验证。

【讨论】:

  • 感谢您,我将 Thread.CurrentPrincipal 替换为 HttpContext.User.Identity 但得到了相同的结果。我认为 MajoB 所说的情况是会话已超时,但身份验证 cookie 仍然经过身份验证。 +1 虽然我肯定从你的回答和汉斯尔曼博客文章的链接中学到了一些东西
【解决方案2】:

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

【讨论】:

  • 谢谢,你说得对,我可以看到会话已超时,但 Principal.Identity 仍然经过身份验证。
【解决方案3】:

可能有点逃避,但在与业务中的其他团队协商后,我将遵循使用公司“单点登录”dll 并使用 Session.KeepAlive 方法的其他站点的模型,所以我将不需要此操作过滤器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    相关资源
    最近更新 更多