IdentityServer4 Authenticates but doesn't Authorize because context.User = null, ASP.NET + Angular 8

Viewed 117

Using:

  1. template Angular + ASP.NET Core application from VS Studio 2019
  2. Angular 8.2.12
  3. .NET 5
  4. MySQL backend

Problem:

Project implements IdentityServer4 for Authentication/Authorization but when adding a certificate to the IDS4 instance Authorization fails as context.User returns null.

Startup.cs:

call the certificate from certificate store:

X509Certificate2 cert = null;
using (var certStore = new X509Store(StoreName.My, StoreLocation.LocalMachine))
{
    certStore.Open(OpenFlags.ReadOnly);
    // var certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint,"THUMBPRINT>, false);
    var certCollection = certStore.Certificates.Find(X509FindType.FindBySubjectName,"localhost", false);
    if (certCollection.Count > 0)
       cert = certCollection[0];
 }

set the certificate on IdentityServer4:

if(cert == null)
{
    services.AddIdentityServer()
        //.AddDeveloperSigningCredential()
        .AddSigningCredential(cert)
        .AddApiAuthorization<ApplicationUser, ApplicationDbContext>();
}
else
{
    services.AddIdentityServer()
        .AddSigningCredential(cert)
        .AddApiAuthorization<ApplicationUser, ApplicationDbContext>();
}

adding Authentication & Authorization:

services.AddAuthentication()                
    .AddIdentityServerJwt();

services.AddScoped<IAuthorizationHandler, UserAuthorizationHandler>();
services.AddAuthorization(options =>
{
      options.AddPolicy("default", policy =>
      {
            policy.RequireAuthenticatedUser();
            //To require the basic user_impersonation scope across the API, you can use:
            //policy.RequirePermissions(
            //    delegated: new[] { "user_impersonation" },
            //    application: new string[0]);
       });
       options.AddPolicy("LocalAuthorizationPolicy", policy => policy.Requirements.Add(new UserRequirement(CustomRoleTypes.SiteAdmin)));           
});

The certificate gets correctly found and assigned to the IDS4 instance. User Authentication proceeds correctly and user gets redirected to the correct location. User is able to call General -non authorized- Controller methods.

When controller method is decorated by [Authorize] the controller returns a 401 error. Because the user is not recognized within the context and returns null. (i.e. AuthorizationHandlerContext as well as IHttpContextAccessor return null for ClaimsPrincipal) The latter does not happen when IDS4 is instantiated using no SigningCredentials.

i.e. when using:

services.AddIdentityServer()   
    .AddApiAuthorization<ApplicationUser, ApplicationDbContext>();

it all works correctly...

I have checked the returned user using the LocalAuthorizationPolicy and determine if there is a user identified in the request context.

I have searched many sites for a walkthrough on the ideal implementation but so far have not been able to find the solution.

I noticed that there are several posts that recommend installing IdentityServer as its own project . But haven't been able to implement that yet...

Any advise would be appreciated.

0 Answers
Related