我有一个使用 Microsoft.AspNetCore.ApiAuthorization.IdentityServer
包通过默认 Microsoft 模板托管的 .NET Core Blazor WebAssembly 应用程序。
我需要添加一个单独的客户端来通过客户端凭据请求访问 token ,以使用服务器端应用程序上的 API Controller 端点,但在 Microsoft 网站或 IdentityServer4 文档上找不到任何有关如何注册它们的文档使用 Microsoft 的实现。
我尝试在单独的 Config.cs
文件中注册客户端,就像处理典型的 IdentityServer4 项目一样:
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" }
}
};
启动:
services.AddIdentityServer()
.AddInMemoryClients(Config.Clients)
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>();
但是,在请求 token 时,这会返回客户端未找到错误:
根据 Microsoft Blazor WebAssembly docs ,API 资源:“WebAssemblyTest.ServerAPI”是在启动时使用 AddIdentityServerJwt()
注册的,所以我不知道如何让它工作。
最佳答案
从此开始工作 answer我可以通过这种方式加载额外的客户端配置:
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" }
});
});
正如答案所述:“ASP.NET Identity 覆盖了 IdentityServer 客户端配置的记录方法”,因此您必须将单个或一组 IdentityServer4.Models.Client
直接传递到 .AddApiAuthorization()
方法。
关于C# .NET Core 托管 Blazor WebAssembly - 将其他客户端添加到 .Server 项目 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61977069/