Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

authentication - AspNet Core Identity - cookie not getting set in production

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.

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.

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);

   ...
}

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.

What am I missing?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

Once I published to an HTTPS site, the cookie started working again.

The reason it was working locally was that I was running in HTTPS locally.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...