dfmetro
dfmetro

Reputation: 4582

How to add Identity Authentication Cookie Timeout in ASP.NET Core 2

I have an existing production ASP.NET Core 1.1 project that I have upgraded to ASP.NET Core 2.

I set the ASP.NET Core identity authentication cookie like this in ASP.NET Core 1.1 to 2 hours

public void ConfigureServices(IServiceCollection services)
{

    services.AddIdentity<ApplicationUser, ApplicationRole>(options =>
    {
        options.Cookies.ApplicationCookie.CookieName = "MyApp";
        options.Cookies.ApplicationCookie.ExpireTimeSpan = TimeSpan.FromMinutes(120);
     })
    .AddEntityFrameworkStores<MyDbContext, Guid>()
    .AddDefaultTokenProviders();

However in ASP.NET 2.0 Core the Cookies property has been removed from the AddIdentity option

Upvotes: 3

Views: 1791

Answers (1)

Sanket
Sanket

Reputation: 19997

If you want to tweak Identity cookies, they're no longer part of IdentityOptions. You will have use services.ConfigureApplicationCookie like this-

    public void ConfigureServices(IServiceCollection services)
    {
        ....
        services.ConfigureApplicationCookie(options => {
            options.CookieName = "MyApp";
            options.ExpireTimeSpan = TimeSpan.FromMinutes(120);
        });
        ....
    }

Note: CookieName property is obsolete and will be removed in a future version. The recommended alternative is Cookie.Domain.

Upvotes: 4

Related Questions