【问题标题】:Can we personalize Session Timeout for Roles in ASP.NET MVC 5我们可以个性化 ASP.NET MVC 5 中角色的会话超时吗
【发布时间】:2014-12-15 12:03:06
【问题描述】:

这个想法是设置不同的会话超时值 ASP.NET MVC 5 和 ASP.NET Identity 中的不同用户角色。

可以吗?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-5 asp.net-identity roles session-timeout


    【解决方案1】:

    如果您想比普通用户更早地启动管理员,这是我在 Identity 中的存根。

    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        // other stuff
        Provider = new CookieAuthenticationProvider
        {
            // this function is executed every http request and executed very early in the pipeline
            // and here you have access to cookie properties and other low-level stuff. 
            // makes sense to have the invalidation here
            OnValidateIdentity = async context =>
            {
                // invalidate user cookie if user's security stamp have changed
                var invalidateBySecirityStamp = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                        validateInterval: TimeSpan.FromMinutes(30),
                        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager));
                await invalidateBySecirityStamp.Invoke(context);
    
                // check if user is in admin role
                var isAdmin = context.Identity.Claims.Any(c => c.Type == ClaimTypes.Role && c.Value == "AdminRoleName");
    
                // check if enough time has passed to invalidate cookie
                var currentUtc = DateTimeOffset.UtcNow;
                if (context.Options != null && context.Options.SystemClock != null)
                {
                    currentUtc = context.Options.SystemClock.UtcNow;
                }
    
                var issuedUtc = context.Properties.IssuedUtc;
                var bootThemOut = (issuedUtc == null);
                if (issuedUtc != null)
                {
                    var timeElapsed = currentUtc.Subtract(issuedUtc.Value);
                    bootThemOut = timeElapsed > TimeSpan.FromMinutes(3); // invalidate admin cookies in 3 minutes
                }
    
                if (isAdmin && bootThemOut)
                {
                    context.RejectIdentity();
                    context.OwinContext.Authentication.SignOut(context.Options.AuthenticationType);
                }
            }
        }
    });            
    

    【讨论】:

    • 我发现这篇文章让我对上面的答案有了更好的理解:ASPNET Identity Cookie Authentication Timeouts timeouts for use with Identity and MVC5: jamessturtevant.com/posts/…
    • 我们如何使用此解决方案实现SlidingExpiration = true 行为?
    • 我阅读了该帖子,然后一步显示 “由于用户在位置 A 的 validateInterval 之后发出请求,他们将被注销并再次提示输入凭据。” .如果我们不想等到 validateInterval 之后怎么办?如果用户在位置 A 更改密码,我希望用户必须立即在位置 B 登录。请问您能帮忙吗?
    • @CodingYoshi 好吧,在密码更新时更改SecurityStamp 字段并将validateInterval 设置为足够小以匹配您的“立即”要求。虽然我不建议将其设置为少于几秒。
    【解决方案2】:

    根据他们的角色,您可以设置超时,即

    HttpContext.Current.Session.Timeout = 20;
    

    按照您之前的问题,您希望动态执行此操作。您可以在会话中自己存储和更新时间,并在基本控制器的OnActionExecuting 上为每个角色设置。

        if (User.IsInRole("Admin"))
        {
            filterContext.HttpContext.Session.Timeout = 
    (int)filterContext.HttpContext.Session["AdminTimeoutThatYouSetSomewhereElseGlobally"];
        }
    

    【讨论】:

    • 它是否适用于 ASP.NET MVC 5 和 ASP.NET Identity?
    • 适用于 Mvc5。你是如何实现身份的?你有基本控制器吗?应该没问题。您对用户的检查可能不同,但会话是相同的。
    • 只有在使用会话时才有效,默认情况下未启用,AFAIK
    猜你喜欢
    • 1970-01-01
    • 2019-05-21
    • 2015-12-19
    • 2011-08-12
    • 2014-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-21
    相关资源
    最近更新 更多