繁体   English   中英

C# .NET 核心托管 Blazor WebAssembly - 将其他客户端添加到.Server 项目 ZDB974238714CA38DE634A7CE1D0

[英]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.

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