ApiAuthorization does not work after upgrading to .NET 5
See original GitHub issueDescribe the bug
I am using .NET Core API Authorization since 3.1 which was working fine until I upgraded my project to .NET 5. After updating, I have been getting NullReferenceException
To Reproduce
ConfigureServices method from Startup.cs
services.AddDbContext<ApplicationDbContext>(options =>
options.UseInMemoryDatabase("Databaase"));
services.AddDefaultIdentity<ApplicationUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentityServer()
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>();
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.RequireHttpsMetadata = false;
options.SaveToken = true;
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateIssuerSigningKey = true,
ValidIssuer = configuration["Jwt:Issuer"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["Jwt:SecretKey"])),
ValidAudience = configuration["Jwt:Audience"],
ValidateAudience = true,
ValidateLifetime = true,
ClockSkew = TimeSpan.Zero // Override the default clock skew of 5 mins
};
//services.AddCors();
});
Configure method from Startup.cs
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
DbContext public class ApplicationDbContext : ApiAuthorizationDbContext<ApplicationUser>
DbContext Constructor public ApplicationDbContext(DbContextOptions options, IOptions<OperationalStoreOptions> operationalStoreOptions, ICurrentUserService currentUserService, IDateTime dateTime) : base(options, operationalStoreOptions) { _currentUserService = currentUserService; _dateTime = dateTime; }
Exceptions
NullReferenceException as: Object reference not set to an instance of an object. Microsoft.Extensions.DependencyInjection.ConfigureApiResources.GetApiResources()+MoveNext() Microsoft.Extensions.DependencyInjection.ConfigureApiResources.Configure(ApiAuthorizationOptions options)
Further technical details
- ASP.NET version 5.0.0
- Visual Studio 2019 (16.8.2) on Windows 10 OS dotnet-info.txt
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:25 (10 by maintainers)
Cool, so everyone is good here now and I can close? I mentioned that to you earlier https://github.com/dotnet/aspnetcore/issues/28456#issuecomment-742685671 @muqeet-ftg but glad you have resolved it out now 😃
@HaoK it did not work earlier, so I didn’t take that into consideration. Now adding both
Key
and.AddIdentityServerJwt()
worked for me. Apologies 😃 … I am really thankful to you and everyone else here who helped me resolving the issue.I am cool, you can close the issue.