Search code examples
asp.net-identityasp.net-core-2.0

Asp Mvc Core 2 Identity not setting cookie name when using AddAuthentication().AddCookie()


Currently, and this works, I am doing the following to setup cookie authentication in an ASP MVC Core 2 app using Identity:

services.ConfigureApplicationCookie(options =>
{
    options.ExpireTimeSpan = TimeSpan.FromDays(1);
    options.SlidingExpiration = true;
    options.LoginPath = "/Account/LogIn";
    options.LogoutPath = "/Account/LogOff";
    options.Cookie.Name = "MyCookieName";
    options.AccessDeniedPath = "/Account/AccessDenied";
});

I want to add JWT to this app and according to the documentation here, I do that by using something like this (based on the same configuration as above):

services.AddAuthentication()
.AddCookie(options =>
{
    options.ExpireTimeSpan = TimeSpan.FromDays(1);
    options.SlidingExpiration = true;
    options.LoginPath = "/Account/LogIn";
    options.LogoutPath = "/Account/LogOff";
    options.Cookie.Name = "MyCookieName";
    options.AccessDeniedPath = "/Account/AccessDenied";
})
.AddJwtBearer(options =>
{ // options });

When I do this (even if I leave off the AddJwtBearer chain) the cookie is no longer given the name I specify. The login process still works and I get a cookie but it is named the default Asp cookie name.

I assume that these two methods of setting the options are the same and the ConfigureApplicationCookie is just a shortcut method to the same thing.

Am I missing something?

Thanks, Brian


Solution

  • Try the following:

    services.AddAuthentication()
            .AddJwtBearer(options =>
            {
                // Jwt options.
            });
    
    services.ConfigureApplicationCookie(options =>
    {
        // Cookie settings
    });