![](/img/trans.png)
[英]Call an foreign API from a Blazor WebAssembly (WASM) Project (**NOT** ASP.Net Core, hosted)
[英]C# .NET Core Hosted Blazor WebAssembly - Add additional clients to .Server project API
我有一个 .NET Core 托管 Blazor WebAssembly 应用程序,来自默认 Microsoft 模板,使用Microsoft.AspNetCore.ApiAuthorization.IdentityServer
package。
我需要添加一个单独的客户端以通过客户端凭据请求访问令牌,以在服务器端应用程序上使用 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>();
但是,这在请求令牌时会返回客户端未找到错误:
根据 Microsoft Blazor WebAssembly 文档,API 资源:“WebAssemblyTest.ServerAPI”是在启动时使用AddIdentityServerJwt()
注册的,所以我不知道如何让它工作。
从这个答案开始,我能够以这种方式加载我的附加客户端配置:
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()
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.