[英]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.我有一个 .NET Core 托管 Blazor WebAssembly 应用程序,来自默认 Microsoft 模板,使用
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.我需要添加一个单独的客户端以通过客户端凭据请求访问令牌,以在服务器端应用程序上使用 API controller 端点,但在 Microsoft 网站或 IdentityServer4 文档上找不到任何有关如何注册它们的文档,因为它正在使用 Microsoft 的实现.
I have tried registering the client in a separate Config.cs
file as you would do with a typical IdentityServer4 project:我已经尝试在单独的
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" }
}
};
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.根据 Microsoft Blazor WebAssembly 文档,API 资源:“WebAssemblyTest.ServerAPI”是在启动时使用
AddIdentityServerJwt()
注册的,所以我不知道如何让它工作。
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.正如答案所述:“ASP.NET Identity 覆盖了 IdentityServer 客户端配置的文档化方法”,因此您必须将单个或一组
IdentityServer4.Models.Client
直接传递给.AddApiAuthorization()
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.