.Net Core Identity AuthenticationCookie 不工作
.Net Core Identity AuthenticationCookie not working
我最近 运行 我的 net5.0 应用程序(从 net core 3.1 升级而来)出现问题。我正在使用 Microsoft.AspNetCore.Identity 登录,它工作得很好。将它部署到生产机器后,登录仍然有效,我收到了我的 cookie。但是在第二天调用 url 之后,尽管有我的身份验证 cookie(有效期为 30 天),但我没有登录。在我托管在 IIS 上的本地计算机上,它工作正常。以下是我的部分代码:
Startup.cs:
配置服务
services.ConfigureApplicationCookie(options =>
{
options.ExpireTimeSpan = TimeSpan.FromDays(30);
options.SlidingExpiration = true;
options.LoginPath = new PathString("/Account/Login");
options.LogoutPath = new PathString("/Account/Logoff");
options.Cookie.Name = "MyApplication";
options.Cookie.IsEssential = true;
});
我正在使用 ProtectPersonalData 属性通过密钥环加密数据库中的用户数据:
services.AddIdentity<ApplicationUser, IdentityRole>(config =>
{
config.SignIn.RequireConfirmedEmail = true;
config.Stores.ProtectPersonalData = true;
config.Stores.MaxLengthForKeys = 128;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
配置
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
这是 Firefox 中的 cookie:
[饼干][1]
根据我的理解,这个 cookie 应该在针对该域的每个请求时发送到服务器。即使 Firefox 声明,它是在我请求的确切时间最后访问的,但它没有任何效果。
如果您需要任何进一步的代码,请告诉我。任何帮助表示赞赏!
[1]: https://i.stack.imgur.com/UTSuR.png
似乎我的代码一切正常。错误在 IIS 配置中。当我使用 DataProtection 时,ApplicationPool 应该将“load userprofile”设置为 true,否则它无法存储密钥环并且在应用 shutdown/restart 后数据会丢失。在生产服务器上更改后,它现在似乎可以正常工作了。
我最近 运行 我的 net5.0 应用程序(从 net core 3.1 升级而来)出现问题。我正在使用 Microsoft.AspNetCore.Identity 登录,它工作得很好。将它部署到生产机器后,登录仍然有效,我收到了我的 cookie。但是在第二天调用 url 之后,尽管有我的身份验证 cookie(有效期为 30 天),但我没有登录。在我托管在 IIS 上的本地计算机上,它工作正常。以下是我的部分代码:
Startup.cs:
配置服务
services.ConfigureApplicationCookie(options =>
{
options.ExpireTimeSpan = TimeSpan.FromDays(30);
options.SlidingExpiration = true;
options.LoginPath = new PathString("/Account/Login");
options.LogoutPath = new PathString("/Account/Logoff");
options.Cookie.Name = "MyApplication";
options.Cookie.IsEssential = true;
});
我正在使用 ProtectPersonalData 属性通过密钥环加密数据库中的用户数据:
services.AddIdentity<ApplicationUser, IdentityRole>(config =>
{
config.SignIn.RequireConfirmedEmail = true;
config.Stores.ProtectPersonalData = true;
config.Stores.MaxLengthForKeys = 128;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
配置
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
这是 Firefox 中的 cookie: [饼干][1]
根据我的理解,这个 cookie 应该在针对该域的每个请求时发送到服务器。即使 Firefox 声明,它是在我请求的确切时间最后访问的,但它没有任何效果。
如果您需要任何进一步的代码,请告诉我。任何帮助表示赞赏!
[1]: https://i.stack.imgur.com/UTSuR.png
似乎我的代码一切正常。错误在 IIS 配置中。当我使用 DataProtection 时,ApplicationPool 应该将“load userprofile”设置为 true,否则它无法存储密钥环并且在应用 shutdown/restart 后数据会丢失。在生产服务器上更改后,它现在似乎可以正常工作了。