AspNet Core身份-未在生产环境中设置Cookie [英] AspNet Core Identity - cookie not getting set in production
问题描述
我有一个.NET Core 2 Web应用程序,并且我想使用ASP.NET Identity来验证我的用户.在.NET Core 1.x上,我的代码运行正常.
I have a .NET Core 2 web app and I want to use ASP.NET Identity to authenticate my users. On .NET Core 1.x, my code was working fine.
我迁移到.NET Core 2,并且在Visual Studio中本地运行时身份验证有效.但是当我部署到实时环境中时,身份验证停止工作:未在生产环境中设置身份验证Cookie.
I migrated to .NET Core 2, and authentication works when running locally in Visual Studio. But when I deploy to a live environment, authentication stops working: the authentication cookie isn't being set in production.
我的Startup.cs代码如下:
My Startup.cs code looks like this:
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentity<AppUser, RavenDB.IdentityRole>()
.AddDefaultTokenProviders();
...
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
...
app.UseAuthentication();
}
要登录,我的代码如下:
To sign in, my code looks like this:
public async Task<ActionResult> SignIn(...)
{
var user = ...; // Load the User from the database.
await this.signInManager.SignInAsync(user, isPersistent: true);
...
}
此代码在本地工作:设置了ASP.NET身份验证cookie.但是,当我将其部署到Azure中的生产环境时,cookie永远不会被设置.
This code works locally: the ASP.NET Identity auth cookie is set. However, when I deploy this to production enviro in Azure, the cookie never gets set.
我想念什么?
推荐答案
我解决了这个问题.归结为HTTPS:看来signInManager.SignInAsync(...)设置了仅HTTPS的cookie.我最初是在非HTTPS网站上发布以进行测试的.
I solved the problem. It boiled down to HTTPS: it appears that signInManager.SignInAsync(...) sets a cookie that is HTTPS-only. I was publishing to a non-HTTPS site initially for testing.
一旦我发布到HTTPS站点,该cookie就会再次开始工作.
Once I published to an HTTPS site, the cookie started working again.
它在本地运行的原因是我在本地运行HTTPS.
The reason it was working locally was that I was running in HTTPS locally.
这篇关于AspNet Core身份-未在生产环境中设置Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!