Sam
Sam

Reputation: 30388

Naming cookies in ASP.NET 5

In my ASP.NET 5 MVC 6 app, I'm using ONLY the cookie middleware without ASP.NET Identity -- as described in this document: https://docs.asp.net/en/latest/security/authentication/cookie.html

I'm also using social authentication i.e. Facebook, Google, etc. So the user authenticates through the social network, then I use the cookie middleware to manage user's session.

In the app, I need to create two cookies i.e. a temp cookie before a new user completes registration and a regular cookie once the registration is completed.

I want to name these cookies and be able to look for them by their names. How do I do that?

Upvotes: 1

Views: 1437

Answers (1)

Kévin Chalet
Kévin Chalet

Reputation: 42000

You can replace the default cookie name directly in the middleware options:

app.UseCookieAuthentication(options => {
    options.AuthenticationScheme = "Instance1";
    options.CookieName = "MyCookie";
});

app.UseCookieAuthentication(options => {
    options.AuthenticationScheme = "Instance2";
    options.CookieName = "MyOtherCookie";
});

To retrieve the ClaimsPrincipal instance associated with a particular scheme, use AuthenticateAsync:

var principal = await context.Authentication.AuthenticateAsync("Instance2");

Upvotes: 2

Related Questions