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?
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.
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.
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.
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With