Welcome to OStack Knowledge Sharing 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...