question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ApiAuthorization does not work after upgrading to .NET 5

See original GitHub issue

Describe 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:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:25 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
HaoKcommented, Jan 12, 2021

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 😃

1reaction
muqeet-ftgcommented, Jan 12, 2021

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does ApiAuthorization not work in .NET 5
I am using .NET Core API Authorization since version 3.1, which was working fine until I upgraded my project to .NET 5. After...
Read more >
App not working after upgrading from .Net Core 3.0 to . ...
Hi, I upgraded my web app to .Net 5.0 and it is no longer running on appservice. I am getting HTTP Error 500.31...
Read more >
[Fix]-Why does ApiAuthorization not work in .NET 5
In case someone is looking for the solution for this issue, as per the Github link provided in update, the issue was resolved...
Read more >
ASP.NET Core 5 and Angular: Full-stack web development with ...
Full-stack web development with .NET 5 and Angular 11, 4th Edition Valerio De Sanctis. • Implement ASP.NET Core Identity and IdentityServer to add...
Read more >
Asp Net Core - Rest API Authorization with JWT (Roles Vs ...
Role-based authorization requires first identifying the user, then ascertaining the roles to which the user is assigned, and finally comparing ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found