ASP.NET Core 2.0 introduces a new model for authentication which requires some changes when upgrading your existing ASP.NET Core 1.x applications to 2.0.
In ASP.NET Core 1.x, every auth scheme had its own middleware, and startup looked something like this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory) { | |
app.UseCookieAuthentication(new CookieAuthenticationOptions | |
{ | |
AuthenticationScheme = "Cookies" | |
}); | |
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions | |
{ | |
AuthenticationScheme = "oidc", | |
SignInScheme = "Cookies", | |
Authority = "http://localhost:5000", | |
RequireHttpsMetadata = false, | |
ClientId = "frontendid", | |
SaveTokens = true, | |
TokenValidationParameters = new TokenValidationParameters | |
{ | |
NameClaimType = "name", | |
RoleClaimType = "role", | |
} | |
}); | |
} |
In ASP.NET Core 2.0, there is now only a single Authentication middleware, and each authentication scheme is registered during ConfigureServices() instead of during Configure():
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ConfigureServices(IServiceCollection services) { | |
services.AddAuthentication(options => | |
{ | |
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; | |
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme; | |
}) | |
.AddCookie() | |
.AddOpenIdConnect(options => | |
{ | |
options.Authority = "http://localhost:5000"; | |
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; | |
options.RequireHttpsMetadata = false; | |
options.ClientId = "frontendid" | |
options.SaveTokens = true; | |
options.TokenValidationParameters = new TokenValidationParameters | |
{ | |
NameClaimType = "name", | |
RoleClaimType = "role", | |
}; | |
}); | |
} | |
public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory) { | |
app.UseAuthentication(); | |
} |
More information: https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/identity-2x