Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net Identity and cookie names

We have 2 applications that are both using Asp.Net Identity for security.

They have nothing to do with each other but I happen to be a developer on both projects.

I'm facing a quite annoying issue with the Cookie name. If I go to app1 and log in then to app2 and log in, I get disconnected from app1.

My wild guess is that it is because the 2 applications are sharing the same cookie name.

So for the ease of development and also because I think it is nicer I'm looking for a way to change the name of the cookie.

Any clue?

like image 697
Georges Legros Avatar asked Jul 30 '14 13:07

Georges Legros


People also ask

What is AspNet ApplicationCookie?

AspNet. ApplicationCookie basically is created when you use cookie authentication in your application. This cookie is created by the server on user request and is stored by the browser. AspNet. ApplicationCookie gets sent with each subsequent request to inform the server the identity of the logged in user.

What is CookieAuthenticationDefaults AuthenticationScheme?

AuthenticationScheme passed to AddAuthentication sets the default authentication scheme for the app. AuthenticationScheme is useful when there are multiple instances of cookie authentication and the app needs to authorize with a specific scheme. Setting the AuthenticationScheme to CookieAuthenticationDefaults.

What is cookies in ASP.NET Core?

Cookies are key-value pair collections where we can read, write and delete using key. In ASP.NET, we can access cookies using httpcontext. current but in ASP.NET Core, there is no htttpcontext.

What is ConfigureApplicationCookie?

ConfigureApplicationCookie(Cookie settings for Application) It contains setting options related to application's cookie. It has following properties. Cookie.Name. It is a name of the cookie. Default value is "AspNetCore.


1 Answers

OK, found it.

By default, VS and Identity will create a file in App_Start named Startup.Auth.cs.

This file contains the following code

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/Account/Login"),
    Provider = new CookieAuthenticationProvider
    {
        OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
            validateInterval: TimeSpan.FromMinutes(30),
            regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
    }
});

To fix our problem, we have to set the CookieName property of the CookieAuthenticationOptions

CookieName = "my-very-own-cookie-name"

That's it; nothing more.

Cheers!

like image 168
Georges Legros Avatar answered Oct 11 '22 15:10

Georges Legros