Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net Core - Prevent Session cookie conflict between same domain applications

I'm using ASP.Net Core 2.2.

By default, session cookie is stored in a cookie named .AspNetCore.Session on a specific domain (e.g: mydomain.com).

In my case I have multiple .net core applications under the domain. mydomain.com/Module1, mydomain.com/Module2, etc...

With this scenario, all the applications share the same cookie for their session. The consequence is that an application try to read the session of the other and generate a warning in the logs:

Error unprotecting the session cookie. System.Security.Cryptography.CryptographicException: The key {...} was not found in the key ring.

Although It's just a warning and session seems to working fine on each application, I wanted to know the proper way to handle this situation.

Thx.

like image 662
maxence51 Avatar asked Apr 23 '19 07:04

maxence51


People also ask

What is SameSite cookie .NET core?

SameSite is an IETF draft standard designed to provide some protection against cross-site request forgery (CSRF) attacks. Originally drafted in 2016, the draft standard was updated in 2019.

What is ASP.NET ApplicationCookie?

AspNet. ApplicationCookie - is created when cookie authentication is used in the application. This cookie is created by the server on user request and is stored by the browser.

What is CookiePolicyOptions?

CookieAuth: CookiePolicyOptions provides programmatic configuration for the CookiePolicyMiddleware. services. Configure<CookiePolicyOptions>(options => { options. HttpOnly = true; ... }); This part is a set up of the Cookie policy in terms of privacy, GDPR(for Europe) and other policies.


1 Answers

A solution that I've found is to change the session cookie name for each application:

In Startup / Configure() :

app.UseSession(new SessionOptions() { Cookie = new CookieBuilder() { 
    Name = ".AspNetCore.Session.MyApp1"}});
like image 187
maxence51 Avatar answered Nov 15 '22 06:11

maxence51