简体   繁体   中英

C# .NET Core Hosted Blazor WebAssembly - Add additional clients to .Server project API

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM