【问题标题】:Users log out very quickly用户注销非常快
【发布时间】:2021-01-17 01:50:03
【问题描述】:

我正在使用 ASP.NET 身份成员资格。这是 Startup.Auth.cs 代码:

 app.CreatePerOwinContext(EFDbContext.Create);
        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("/Account/Login"),  
            ExpireTimeSpan = TimeSpan.FromHours(3),
            CookieName = "MyLoginCookie",

            Provider = new CookieAuthenticationProvider
            {
                // Enables the application to validate the security stamp when the user logs in.
                // This is a security feature which is used when you change a password or add an external login to your account.  
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, User>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))

            }
        });

如您所见,我已将 expiretimespan 设置为 3 小时,但在生产服务器上它不起作用;大约十分钟后到期。当我检查元素时 MyLoginCookie 仍然存在。在本地主机上它工作正常。为什么在生产服务器上有问题?我需要设置 CookieDomain 吗?

【问题讨论】:

  • It expires about in ten minutes - 在什么情况下会发生这种情况?是 10 分钟不活动还是客户端正在积极执行请求并且在初始登录后 10 分钟仍然过期?浏览器中 cookie 的到期日期/时间是什么时候(应该可以通过您的浏览器开发工具查看)?您尝试过哪些浏览器? on production server it doesn't work - 它在哪里按预期工作(仅在本地或在测试服务器上)?您还可以提供什么来提供更多见解?
  • (制作)如果用户在十分钟内没有发送http请求,他就会退出。在本地我大约 30 分钟处于非活动状态,但是当我回来时,我直到登录。我正在使用 google chrome,并且在本地和生产服务器上检查元素 MyLoginCookie 过期时间为 N/A
  • 你看过this write up了吗?它很好地解释了超时的差异和预期行为。
  • 是的,我读过。也许这是一个iis问题,但不知道。它在本地运行良好:(
  • 没有任何想法? ://

标签: c# asp.net-mvc identity


【解决方案1】:

用户注销的原因是表单身份验证数据和视图状态数据的验证错误。它可能由于不同的原因发生,包括在托管服务中使用网络农场。您应该在您的项目webconfig 中检查&lt;machineKey&gt;。检查here 了解详情。如果您的webconfig 中没有&lt;machineKey&gt;,请尝试在您的webconfig 中&lt;system.web&gt; 之后添加这段代码:

    <machineKey 
    validationKey="AutoGenerate,IsolateApps"
    decryptionKey="AutoGenerate,IsolateApps"
    validation="HMACSHA256"
    decryption="Auto"
    />

另一个选项是在 webconfig 中使用生成的 ASP.NET 机器密钥。有一些在线工具,我推荐的是thisthis

【讨论】:

  • 为我省去了很多困惑!我发现我确实需要生成的机器密钥,自动生成它几乎/没有区别。
猜你喜欢
  • 2015-01-25
  • 1970-01-01
  • 1970-01-01
  • 2015-08-18
  • 2021-11-23
  • 1970-01-01
  • 2018-07-27
  • 1970-01-01
  • 2020-10-05
相关资源
最近更新 更多