I have a .NET Core hosted Blazor WebAssembly app from the default Microsoft template using the Microsoft.AspNetCore.ApiAuthorization.IdentityServer
package.
I need to add a separate client to request access tokens via client credentials to use the API controller endpoints on the server-side application but cannot find any documentation on how to register them on either the Microsoft website or IdentityServer4 docs as it is using Microsoft's implementation.
I have tried registering the client in a separate Config.cs
file as you would do with a typical IdentityServer4 project:
public static IEnumerable<IdentityServer4.Models.Client> Clients =>
new List<IdentityServer4.Models.Client>
{
new IdentityServer4.Models.Client
{
ClientId = "web_id",
ClientSecrets = { new Secret("web_id".ToSha256()) },
AllowedGrantTypes = GrantTypes.ClientCredentials,
AllowedScopes = { "WebAssemblyTest.ServerAPI" }
}
};
Startup:
services.AddIdentityServer()
.AddInMemoryClients(Config.Clients)
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>();
However this returns a client not found error when requesting a token:
Accoring to Microsoft Blazor WebAssembly docs, the API resource: "WebAssemblyTest.ServerAPI" is registered using the AddIdentityServerJwt()
in startup so I have no idea how to get this working.
Working from this answer I was able to load my additional client config this way:
services.AddIdentityServer()
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>(options =>
{
options.Clients.Add(new IdentityServer4.Models.Client
{
ClientId = "web_id",
ClientSecrets = { new Secret("web_id".ToSha256()) },
AllowedGrantTypes = GrantTypes.ClientCredentials,
AllowedScopes = { "WebAssemblyTest.ServerAPI" }
});
});
As the answer states: "ASP.NET Identity overrides the documented method for IdentityServer Clients configuration" so you have to either pass a single or array of IdentityServer4.Models.Client
directly into the .AddApiAuthorization()
method.