ASP.NET的身份转变后ExpireTimeSpan ConfigureAuth(ASP.NET

2019-10-21 08:58发布

我的工作,我最近从一个自定义的身份验证解决方案,以ASP.NET身份迁移的Web应用程序。 在我们的应用程序的某些部分,长工序进行,可以采取一个小时运行。 一般情况下,我们希望用户在非活动30分钟后自动注销。 然而,长期的过程发生在哪里的屏幕,我们要扩大自动注销窗口,以便在该过程完成之前,他们没有得到踢出。 我目前正在如何使用Identity做到这一点挣扎。 我目前使用我的Startup.Auth.cs文件下面的代码:

public partial class Startup
{
    // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301883
    public void ConfigureAuth(IAppBuilder app)
    {
        // Configure the user manager and signin manager to use a single instance per request
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

        // Enable the application to use a cookie to store information for the signed in user
        // and to use a cookie to temporarily store information about a user logging in with a third party login provider
        // Configure the sign in cookie
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Login.aspx"),
            ExpireTimeSpan = TimeSpan.FromMinutes(30),
            SlidingExpiration = true,
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity =
                    SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser, int>(
                        TimeSpan.FromMinutes(30),
                        (manager, user) => user.GenerateUserIdentityAsync(manager),
                        userIdentity => userIdentity.GetUserId<int>())
            }
        });
    }
}

我目前没有看到任何方式修改ExpireTimeSpan值后ConfigureAuth已经被调用。 我希望能在值更改Page_Load事件处理程序从Web窗体页。 有谁知道的一种方式?

Answer 1:

里面OnValidateIdentity

又名:

OnValidateIdentity = delegate(CookieValidateIdentityContext context) {
            var stampValidator = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser, int>(
                validateInterval: TimeSpan.FromMinutes(30),
                regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager),
                getUserIdCallback: (id) => (id.GetUserId<int>())
            );
            Task result = stampValidator.Invoke(context);

            int my_custom_minutes = 60; // run your logic here.
                // set it at GLOBAL level for all (future) users.
                context.Options.ExpireTimeSpan = TimeSpan.FromMinutes( my_custom_minutes );
                // set it for the current user only.
                context.Properties.ExpiresUtc = context.Properties.IssuedUtc.Value.AddMinutes( my_custom_minutes );

            return result;
}


文章来源: ASP.NET Identity Change ExpireTimeSpan after ConfigureAuth