Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 635
Judah Gabriel Himango Avatar asked Sep 05 '17 16:09

Judah Gabriel Himango


People also ask

What is ASP.NET Core session cookie?

Session uses a cookie to track and identify requests from a single browser. By default, this cookie is named . AspNetCore. Session , and it uses a path of / . Because the cookie default doesn't specify a domain, it isn't made available to the client-side script on the page (because HttpOnly defaults to true ).

What is ASP.NET Core identity?

ASP.NET Core Identity: Is an API that supports user interface (UI) login functionality. Manages users, passwords, profile data, roles, claims, tokens, email confirmation, and more.

What is cookie authentication in .NET core?

ASP.NET Core provides a cookie authentication mechanism which on login serializes the user details in form of claims into an encrypted cookie and then sends this cookie back to the server on subsequent requests which gets validated to recreate the user object from claims and sets this user object in the HttpContext so ...

What is Aspnet cookies cookie?

A cookie is a small bit of text that accompanies requests and pages as they go between the Web server and browser. The cookie contains information the Web application can read whenever the user visits the site.


2 Answers

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.

like image 170
Judah Gabriel Himango Avatar answered Oct 31 '22 12:10

Judah Gabriel Himango


Had same problem with Chrome 60+. Cookie did not want to set on HTTP site or even HTTPS and Cordova.
options.Cookie.SameSite = SameSiteMode.None;
https://github.com/aspnet/Docs/blob/master/aspnetcore/security/authentication/cookie.md
Changing from default value (Lax) to None fixed it for me.

like image 3
aMerkuri Avatar answered Oct 31 '22 13:10

aMerkuri