什么设置系统.安全.主体.身份.在会话超时后进行身份验证

本文关键字:超时 身份验证 会话 系统 设置 安全 主体 身份 什么 | 更新日期: 2023-09-27 18:33:42

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

[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,但是当它在操作过滤器中命中时,它仍然是 true。(我知道会话已超时,因为我在 Global.asax 中的Session_End上放置了一个断点,并且首先命中了这个断点)。

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

任何帮助,不胜感激。

什么设置系统.安全.主体.身份.在会话超时后进行身份验证

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

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

会话和身份验证cookie是不同的东西。您可以拥有仍然经过身份验证但会话已过期的用户。看看这篇文章:asp.net cookie,身份验证和会话超时<</p>

div class="answers">

可能有点麻烦,但在咨询了业务中的其他团队后,我将遵循使用公司"单点登录"dll并使用Session.KeepAlive方法的其他站点的模型,因此我不需要此操作过滤器。