【问题标题】:C# .NET Core Hosted Blazor WebAssembly - Add additional clients to .Server project APIC# .NET Core 托管 Blazor WebAssembly - 将其他客户端添加到 .Server 项目 API
【发布时间】:2020-09-10 14:17:40
【问题描述】:

我有一个使用 Microsoft.AspNetCore.ApiAuthorization.IdentityServer 包的默认 Microsoft 模板中托管的 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() 注册的,所以我不知道如何让它工作。

【问题讨论】:

    标签: c# identityserver4 blazor webassembly blazor-client-side


    【解决方案1】:

    通过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() 方法。

    【讨论】:

    • 感谢分享,这是我需要的缺失部分
    猜你喜欢
    • 2020-09-25
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 2021-06-25
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多