用户很快就会退出

use*_*731 5 c# asp.net-mvc identity

我正在使用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))

            }
        });
Run Code Online (Sandbox Code Playgroud)

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

Had*_*dee 8

用户注销的原因是表单身份验证数据和视图状态数据的验证错误.它可能由于不同的原因而发生,包括在托管服务中使用Web场<machineKey>.您应该检查您的项目webconfig.点击此处查看详细信息.如果您没有<machineKey>,请webconfig尝试<system.web>在webconfig中添加以下代码:

    <machineKey 
    validationKey="AutoGenerate,IsolateApps"
    decryptionKey="AutoGenerate,IsolateApps"
    validation="HMACSHA256"
    decryption="Auto"
    />
Run Code Online (Sandbox Code Playgroud)

另一个选项是在webconfig中使用生成的ASP.NET Machine Key.有一些在线工具,我推荐的是这个这个.

  • 在那里为我省去了很多困惑!我发现我确实需要生成的机器密钥,自动生成它几乎没有/没有区别。 (2认同)