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

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

我有一个使用 Microsoft.AspNetCore.ApiAuthorization.IdentityServer 包通过默认 Microsoft 模板托管的 .NET Core Blazor WebAssembly 应用程序。

我需要添加一个单独的客户端来通过客户端凭据请求访问 token ,以使用服务器端应用程序上的 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>();

但是,在请求 token 时,这会返回客户端未找到错误:

enter image description here

根据 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 客户端配置的记录方法”,因此您必须将单个或一组 IdentityServer4.Models.Client 直接传递到 .AddApiAuthorization() 方法。

关于C# .NET Core 托管 Blazor WebAssembly - 将其他客户端添加到 .Server 项目 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61977069/

相关文章:

ajax - 在身份服务器 4 中处理从 MVC 客户端到资源 api 的 ajax 调用中的身份验证

c# - 返回类型错误的 Blazor 任务

c# - 如何向服务器端 Blazor 项目添加 Controller (而非 View )支持

c# - 优化 C# 代码片段

c# - Xamarin : Get MvxRecyclerView working

c# - 进程内和进程外托管的 ASP .NET Core 3 + Identity Server 问题

c# - 如何将 IsActiveAsync 失败信息传递给用户和客户端?

c# - 更改 Azure 应用服务以显示与用户设置存储值不同的时区?

c# - 从不同的线程关闭主应用程序

c# - Windows 窗体并行绘图