我有一个默认的Microsoft模板,使用Microsoft.AspNetCore.ApiAuthorization.IdentityServer
包从.NET Core托管Blazor WebAssembly应用程序。
我需要添加一个单独的客户端以通过客户端凭据来请求访问令牌,以使用服务器端应用程序上的API控制器端点,但是在使用时无法在Microsoft网站或IdentityServer4文档上找到任何有关如何注册它们的文档。微软的实现。
我已经尝试将客户端注册到单独的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 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 Clients配置的文档方法,因此您必须将IdentityServer4.Models.Client
的单个或数组直接传递给.AddApiAuthorization()
方法。