I just updated my dotnet core webapi application from netcoreapp1.0 to netcoreapp2.0. I am using openiddict for authentication and authorization based on this sample.
ConfigureServices method :
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddMvc().AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
});
services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseSqlServer(@"Server=SERVER1;Database=DB1;User Id=BLAHBLAH;Password=BLAHBLAHBLAH;");
options.UseOpenIddict();
});
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.Configure<IdentityOptions>(options =>
{
options.ClaimsIdentity.UserNameClaimType = OpenIdConnectConstants.Claims.Name;
options.ClaimsIdentity.UserIdClaimType = OpenIdConnectConstants.Claims.Subject;
options.ClaimsIdentity.RoleClaimType = OpenIdConnectConstants.Claims.Role;
});
services.AddOpenIddict(options =>
{
options.AddEntityFrameworkCoreStores<ApplicationDbContext>();
options.AddMvcBinders();
options.EnableTokenEndpoint("/connect/token");
options.AllowPasswordFlow();
options.DisableHttpsRequirement();
options.SetAccessTokenLifetime(TimeSpan.FromMinutes(5));
});
services.AddAuthentication()
.AddOAuthValidation();
}
Configure method :
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseCors(b => b.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
app.UseOpenIdConnectServer(configuration => {
configuration.AllowInsecureHttp = true;
configuration.Provider = new AuthorizationProvider();
});
app.UseAuthentication();
app.UseMvc();
}
The AuthorizationProvider class :
public sealed class AuthorizationProvider : OpenIdConnectServerProvider
{
public AuthorizationProvider()
{
}
public override async Task ApplyTokenResponse(ApplyTokenResponseContext context)
{
if (string.IsNullOrEmpty(context.Error))
{
var role = context.Ticket.Principal.Claims.FirstOrDefault(q => q.Type == OpenIdConnectConstants.Claims.Role).Value;
var userName = context.Ticket.Principal.Claims.FirstOrDefault(q => q.Type == OpenIdConnectConstants.Claims.Name).Value;
context.Response["role"] = role;
context.Response["userName"] = userName;
context.Response[".issued"] = DateTime.Now.ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss 'GMT'");
context.Response[".expires"] = DateTime.Now.AddHours(8).ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss 'GMT'");
}
return;
}
}
The following code is not working :
app.UseOpenIdConnectServer(configuration => {
configuration.AllowInsecureHttp = true;
configuration.Provider = new AuthorizationProvider();
});
It says 'IApplicationBuilder' does not contain a definition for 'UseOpenIdConnectServer' and no extension method 'UseOpenIdConnectServer' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)
How do I resolve it? What is the alternative method to add a custom provider?
ASP.NET Core 2.0 has a new model for authentication and Identity which simplifies configuration by using services and below is the migration guide
Migrating Authentication and Identity to ASP.NET Core 2.0
in Configure method change this
To this
and in ConfigureServices add the below code