3

Creating a new Blazor WebAssembly App with Microsoft Visual Studio 2019 Version 16.11.3 with these specifications: Target Framework .NET 5.0, Authentication Type Individual Accounts and ASP.NET Core Hosted:

enter image description here

I then only edited appsettings.Development.json and removed the following:

  "IdentityServer": {
    "Key": {
      "Type": "Development"
    }
  }

This gives the exception:

NullReferenceException: Object reference not set to an instance of an object. Microsoft.Extensions.DependencyInjection.IdentityServerBuilderConfigurationExtensions+<>c.<AddSigningCredentials>b__10_2(IServiceProvider sp)

In Startup.cs I then tried to add AddDeveloperSigningCredential like this:

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

However this gives me the same error. I then tried .AddDeveloperSigningCredential(true);, same error.

I then tried modified code from Microsoft Docs:

X509Certificate2 certificate = null;
var bytes = File.ReadAllBytes(@"C:\Test\test.pfx");
certificate = new X509Certificate2(bytes, "SuperSecurePassword123!@");

services.AddIdentityServer()
    .AddApiAuthorization<ApplicationUser, ApplicationDbContext>(options => { })
    .AddSigningCredential(certificate);

https://learn.microsoft.com/en-us/aspnet/core/blazor/security/webassembly/hosted-with-identity-server?view=aspnetcore-5.0&tabs=visual-studio#host-in-azure-app-service-with-certificate-token-signing

Same error.

I then tried getting certificate from Store:

X509Certificate2 certificate = null;
using (X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser))
{
    store.Open(OpenFlags.ReadOnly);
    X509Certificate2Collection certificates = store.Certificates.Find(X509FindType.FindByThumbprint, "MyThumbprint", false);

    if (certificates.Count > 0)
        certificate = certificates[0];
}

services.AddIdentityServer()
    .AddApiAuthorization<ApplicationUser, ApplicationDbContext>(options => { })
    .AddSigningCredential(certificate);

Same error even though I have verified that an actual certificate is picked up in both cases:

enter image description here

I then tried removing every configuration with IdentityServer from appsettings.json

  "IdentityServer": {
    "Clients": {
      "BlazorTestWithIdentityServer.Client": {
        "Profile": "IdentityServerSPA"
      }
    }
  },

And replaced it with this:

services.AddIdentityServer()
    .AddApiAuthorization<ApplicationUser, ApplicationDbContext>(options =>
    {
        options.Clients.AddIdentityServerSPA("BlazorTestWithIdentityServer.Client", builder =>
        {
            builder.WithRedirectUri("/authentication/login-callback");
            builder.WithLogoutRedirectUri("/authentication/logout-callback");
        });
    })
    .AddDeveloperSigningCredential();

Still causing the same error.

Running the code below:

services.AddIdentityServer()
    .AddApiAuthorization<ApplicationUser, ApplicationDbContext>(options =>
    {
        options.Clients.AddIdentityServerSPA("BlazorTestWithIdentityServer.Client", builder =>
        {
            builder.WithRedirectUri("/authentication/login-callback");
            builder.WithLogoutRedirectUri("/authentication/logout-callback");
        });
    });

With appsettings.Development.json:

  "IdentityServer": {
    "Key": {
      "Type": "Development"
    }
  }

Everything works as expected.

How can I get AddDeveloperSigningCredential and AddSigningCredential to work with services.AddIdentityServer()?

Ogglas
  • 62,132
  • 37
  • 328
  • 418

0 Answers0