【问题标题】:ASP.NET Identity Change ExpireTimeSpan after ConfigureAuthConfigureAuth 后 ASP.NET 标识更改 ExpireTimeSpan
【发布时间】:2015-02-01 13:54:55
【问题描述】:

我正在开发一个最近从自定义身份验证解决方案迁移到 ASP.NET Identity 的 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>())
            }
        });
    }
}

在调用ConfigureAuth 之后,我目前看不到任何修改ExpireTimeSpan 值的方法。我希望能够从 Web 表单页面更改 Page_Load 事件处理程序中的值。有人知道方法吗?

【问题讨论】:

  • 是否可以选择只进行 ajax 调用?以便访问会话?
  • @alexo:这是一个有趣的选择。我不知道为什么我没有考虑到这一点。我想我会把它放在我的后兜里,以防我找不到更改身份验证过期时间跨度设置本身的方法。
  • 问题是,如果你在一个地方改变它,它就会到处改变,据我所知,这有点全球化。所以你可能不希望其他页面发生这种情况
  • @alexo:这是一个全局设置,但可以在每次页面加载时更改。例如,我可以让母版页在每次页面加载时将 ExpireTimeSpan 设置为其默认值,然后允许该值在页面加载时被任何希望它不同的页面覆盖。
  • 当您的意思是全局时,您是指会话级别的全局还是服务器级别的全局?因为如果它的服务器级别并且您在一个会话中为特定用户更改它,那么它也会为其他用户更改。据我所知,它在服务器级别是全局的,如果这是您想要的行为,那么这就是答案吗?

标签: c# asp.net owin asp.net-identity-2


【解决方案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;
}

【讨论】:

  • 您的解决方案似乎为特定用户提供了过期时间,但它会持续整个会话。让我知道这是正确的。我正在寻找的是任意延长超时的能力。例如,我们希望在仅查看特定页面时延长超时。在 PageA.aspx 上,将超时时间延长至 1 小时。
  • 应该能够访问 HttpContext,因此您可以检查请求 url 是否匹配页面,并为当前用户扩展它。如果您只想在页面视图期间扩展它,而不是整个 asp 用户会话,则必须以某种方式(我会使用声明)跟踪检测到的页面首次加载时的剩余时间,并且稍后,当加载任何其他页面时,将当前剩余时间(由先前的逻辑更改)减去当时存储的数量(通过声明)。不确定这是否有帮助,但上面的代码是我知道动态更改它的唯一方法
猜你喜欢
  • 2015-07-22
  • 2014-05-22
  • 1970-01-01
  • 1970-01-01
  • 2017-12-13
  • 1970-01-01
  • 2018-10-26
  • 2015-02-10
  • 2014-11-10
相关资源
最近更新 更多