Search code examples
c#asp.netasp.net-mvcasp.net-coreasp.net-identity

Cannot set AuthenticationType in CookieAuthenticationOptions


I'm trying to implement the solution proposed by this user for prevent multiple logins in my application.

Actually I declared in my Startup class, specifically in the Configure method this code:

app.UseCookieAuthentication(new CookieAuthenticationOptions
{

});

the problem is that when I type: AuthenticationType I get nothing displayed, because CookieAuthenticationOptions doesn't have this property, and this is weird because also in the documentation the property doesn't exists anymore.

If I hover the mouse on CookieAuthenticationOptions I can see this namespace: Assembly Microsoft.AspNetCore.Authentication.Cookies.

PS: I'm using ASP.NET CORE


Solution

  • app.UseCookieAuthentication() is deprecated ASP.NET Core 2.X, you should use app.UseAuthentication() in the Configure method instead, however you'll need to configure the authentication inside the ConfigureServices method.

    Using NuGet package Microsoft.AspNetCore.Mvc version 2.1.0 or later it should be configured like this:

    public void ConfigureServices(IServiceCollection services)
    {
        // Add the needed services, e.g. services.AddMvc();
    
        services
            .AddAuthentication(options =>
            {
                options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            })
            .AddCookie(options =>
            {
                // Change the options as needed
            });            
    }
    
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseStaticFiles();
    
        app.UseAuthentication();
    
        app.UseMvc();
    }