简体   繁体   English

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

[英]C# .NET Core Hosted Blazor WebAssembly - Add additional clients to .Server project API

I have a .NET Core hosted Blazor WebAssembly app from the default Microsoft template using the Microsoft.AspNetCore.ApiAuthorization.IdentityServer package.我有一个 .NET Core 托管 Blazor WebAssembly 应用程序,来自默认 Microsoft 模板,使用Microsoft.AspNetCore.ApiAuthorization.IdentityServer package。

I need to add a separate client to request access tokens via client credentials to use the API controller endpoints on the server-side application but cannot find any documentation on how to register them on either the Microsoft website or IdentityServer4 docs as it is using Microsoft's implementation.我需要添加一个单独的客户端以通过客户端凭据请求访问令牌,以在服务器端应用程序上使用 API controller 端点,但在 Microsoft 网站或 IdentityServer4 文档上找不到任何有关如何注册它们的文档,因为它正在使用 Microsoft 的实现.

I have tried registering the client in a separate Config.cs file as you would do with a typical IdentityServer4 project:我已经尝试在单独的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" }
            }
        };

Startup:启动:

services.AddIdentityServer()
            .AddInMemoryClients(Config.Clients)
            .AddApiAuthorization<ApplicationUser, ApplicationDbContext>();

However this returns a client not found error when requesting a token:但是,这在请求令牌时会返回客户端未找到错误:

在此处输入图像描述

Accoring to Microsoft Blazor WebAssembly docs , the API resource: "WebAssemblyTest.ServerAPI" is registered using the AddIdentityServerJwt() in startup so I have no idea how to get this working.根据 Microsoft Blazor WebAssembly 文档,API 资源:“WebAssemblyTest.ServerAPI”是在启动时使用AddIdentityServerJwt()注册的,所以我不知道如何让它工作。

Working from this answer I was able to load my additional client config this way:从这个答案开始,我能够以这种方式加载我的附加客户端配置:

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" }

                    });
                });

As the answer states: "ASP.NET Identity overrides the documented method for IdentityServer Clients configuration" so you have to either pass a single or array of IdentityServer4.Models.Client directly into the .AddApiAuthorization() method.正如答案所述:“ASP.NET Identity 覆盖了 IdentityServer 客户端配置的文档化方法”,因此您必须将单个或一组IdentityServer4.Models.Client直接传递给.AddApiAuthorization()方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Blazor WebAssembly (WASM) 项目调用外国 API(**不是** ASP.Net Core,托管) - Call an foreign API from a Blazor WebAssembly (WASM) Project (**NOT** ASP.Net Core, hosted) 托管在 ASP.NET Core 项目上的 Blazor Webassembly 项目中的身份验证问题 - Problem with Authentification in Blazor Webassembly project which hosted on ASP.NET Core project 没有托管的 Blazor WebAssembly 作为客户端与 .Net Core WebAPI 作为服务器端交互 - Blazor WebAssembly without hosted act as client side interact with .Net Core WebAPI as server side 部署托管到 Azure 的 Blazor WebAssembly 应用 ASP.NET Core - Deploy Blazor WebAssembly App ASP.NET Core hosted to Azure 如何使用 ASP.NET 核心托管发布 Blazor WebAssembly - How to publish Blazor WebAssembly with ASP.NET Core hosted 如何在Blazor WebAssembly项目中使用C# 9? - How to use C# 9 in Blazor WebAssembly project? Blazor 添加一个 ASP.NET Core 时出现 Webassembly 错误 Web API 项目参考 - Blazor Webassembly error when adding a ASP.NET Core Web API Project Reference Blazor WASM Asp.net 核心是否托管 = Blazor 服务器端? - Is Blazor WASM Asp.net core Hosted = Blazor server side? Blazor Webassembly (PWA) 是否支持独立的 session 存储(不在 asp.net 内核中托管)? - Is session storage supported in Blazor Webassembly (PWA) standalone (Not hosted in asp.net core)? Blazor WebAssembly 应用程序与个人帐户和 ASP.NET 核心托管 - 令牌请求 - “错误”:“unauthorized_client” - Blazor WebAssembly App with Individual Accounts and ASP.NET Core Hosted - Token request - "error": "unauthorized_client"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM