2

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

Charanoglu
  • 1,229
  • 2
  • 11
  • 30
  • What version of ASP.NET Core are you using? Note that the linked question is not about ASP.NET **Core** but about classic ASP.NET MVC. Solutions for the latter will generally not work on Core. – poke Sep 02 '18 at 20:31
  • the version of ASP.CORE you are targeting is relevant, yes. The question you're referencing to was likely not ASP.NET Core 2.0+. Note, there was aspnet announcement on this (here is the draft for it): https://github.com/aspnet/Security/issues/1310 – Brett Caswell Sep 02 '18 at 20:32
  • As others have noted the example you're linking to is not for ASP.NET Core. You need to find a more recent example. Also, the entire authentication module was completely overhauled in ASP.NET Core 2.0 so you'll need to find examples for that version. Try starting with the official docs https://learn.microsoft.com/en-us/aspnet/core/security/authentication/?view=aspnetcore-2.1 – Brad Sep 03 '18 at 00:11
  • I'm using `ASP.NET Core 2.1`, and I actually learning it, so I don't no the difference with the previous versions. Someone have a fresh example on this type of situation? – Charanoglu Sep 03 '18 at 08:00

2 Answers2

1

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();
}
Martin
  • 1,634
  • 1
  • 11
  • 24
  • Thanks for the answer Martin, the only thing that I don't understand is how your code can prevent the multiple login, could you explain this? Or maybe you have missed something? – Charanoglu Sep 04 '18 at 11:06
  • This doesn't handle the issue with preventing multiple logins, but only enables you to change the cookie options, which is also needed by looking at the linked question. – Martin Sep 04 '18 at 11:10
  • The problem's that I cannot find the property `AuthenticationType ` inside the `options.`, and also the others seems no more available – Charanoglu Sep 04 '18 at 11:17
  • The AutenticationType is moved into `AddAuthentication` and if I remember correctly is now `DefaultScheme`. For the others look at the `options.Events` inside AddCookie() – Martin Sep 04 '18 at 11:23
0

1- double click on your project

2- in <item group> Add the following line:

<PackageReference Include="Microsoft.AspNetCore.App" />

Kobayashi
  • 2,402
  • 3
  • 16
  • 25