【问题标题】:Identity Server 4 - unauthorized clientIdentity Server 4 - 未经授权的客户端
【发布时间】:2020-01-11 10:40:54
【问题描述】:

我正在努力使用 Net Core 3.0 和 React 进行 Identity Server 4 的基本设置(但这几乎无关紧要)。

我通过dotnet new react -au Individual 生成了新应用,更新了依赖项等,创建的配置基本上是从演示服务器复制的,内容如下:

public static IEnumerable<Client> GetClients()
        {
            return new List<Client>
            {
                // JavaScript Client
                new Client
                {
                    Enabled = true,
                    ClientId = "spa",
                    ClientName = "SPA (Code + PKCE)",

                    RequireClientSecret = false,
                    RequireConsent = false,

                    RedirectUris = { "https://notused" },
                    PostLogoutRedirectUris = { "https://notused" },

                    AllowedGrantTypes = GrantTypes.Code,
                    AllowedScopes = { "openid", "profile", "email", "api" },

                    AllowOfflineAccess = true,
                    RefreshTokenUsage = TokenUsage.ReUse
                },
            };
        }

在我的启动中:

services.AddDefaultIdentity<ApplicationUser>()
                .AddEntityFrameworkStores<ApplicationDbContext>();

            services.AddIdentityServer(o =>
                {
                    o.UserInteraction.ErrorUrl = "/myErrorsHandler";
                    o.Events.RaiseErrorEvents = true;
                    o.Events.RaiseFailureEvents = true;
                    o.Events.RaiseInformationEvents = true;
                    o.Events.RaiseSuccessEvents = true;
                })
                .AddInMemoryApiResources(Config.GetApis())
                .AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>()
                .AddInMemoryClients(Config.GetClients())   ;

然后我在 Postman 中尝试: 并且总是得到:

{"displayMode":null,"uiLocales":null,"error":"unauthorized_client","errorDescription":"Unknown client or client not enabled","requestId":"0HLPL86NBMDRG:00000001","redirectUri":null,"responseMode":null,"clientId":"spa"}

我真的不明白为什么这不起作用。 演示服务器上的相同客户端与 Postman 对话框中的相同客户端可以正常工作。

更新: 我找到了这个文档:https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity-api-authorization?view=aspnetcore-3.0#application-profiles 但我仍然无法让它工作。 它识别客户端,但尽管配置(SPA,IdentityServerSPA)抛出:

{"displayMode":null,"uiLocales":null,"error":"invalid_request","errorDescription":"code challenge required","requestId":"0HLPL8VD22382:00000001","redirectUri":"http://localhost:5000/authentication/login-callback?error=invalid_request&error_description=code%20challenge%20required#_=_","responseMode":"query","clientId":"spa"}

更新 2: 它与配置 JSON 中定义的客户端“工作”,但仅使用 doc 中的预定义模板,但不可能(或没有记录的可能性)禁用 PKCE 以使其工作,例如与邮递员等

【问题讨论】:

  • appsettigs.json中怎么做
  • 未知客户端或客户端未启用表示您在请求中提供了错误的 client_id。而code challenge required表示邮递员不支持PKCEI don't use Postman, but google gives me that
  • 任何帮助更改它以禁用 appsettings.json 中的 PKCE ` "IdentityServer": { "Key": { "Type": "Development" }, "Clients": { "spa": { “个人资料”:“IdentityServerSPA”,} } }`
  • 不确定如何使用配置文件来处理,它们或多或少是硬编码的,但您可以从设置 RequirePkce = false 的位置开始使用 this 配置
  • 它对代码中定义的客户端根本不起作用:/只是由于某种原因没有加载它

标签: c# .net-core asp.net-identity identityserver4


【解决方案1】:

您没有定义client_secret。根据您在客户端配置中提供的代码,您没有设置客户端密码,因此如果未指定客户端密码,则您的客户端无法直接向您的授权 (IDserver) 证明其真实性。这是 PKCE 派上用场的时候,至少您可以保证同一个系统正在执行这两个请求。

我看到您要求禁用 PKCE,这应该是不可能的(我不确定是否可以这样做,但您绝对不应该这样做),因为您正在为 SPA 使用代码身份验证授权。 (这是目前推荐的做事方式)

由于 SPA 是非机密客户端(无法保证机密安全),这意味着任何其他应用程序都可以使用您的 client_id spa 向令牌端点发出请求。为了防止这种情况,我们结合了两件事:

  • 重定向 URI:这会强制将响应代码令牌重定向到以前已知的地址,该地址应该是您的客户端(除非使用主机文件取代您的站点)

  • PKCE:一种旨在保证/authorize/token 请求来自同一个客户端的机制,因此即使有人设法拦截了代码,他/她也不应该能够使用它以换取令牌,因为不知道 PKCE 中使用的原始密钥。

【讨论】:

  • 我不认为这是正确的。您可以在没有秘密的情况下使用客户端。
  • @MU,从安全角度来看,对秘密的担忧是正确的。 PKCE 是秘密的真正替代品,但是在 IdSrv 中,设置是独立的,并且可以出于测试目的禁用秘密要求
  • @MU 无论如何应该是RedirectUris = { "https://notused" },并且它必须与您在邮递员中设置为Callback URL 的Url 匹配,或者您的Web 应用程序中的真正回调路由
  • 我尝试过这样的事情:` services.AddIdentityServer(o => { o.UserInteraction.ErrorUrl = "/myErrorsHandler"; }) .AddApiAuthorization(c => { c .Clients = new ClientCollection(GetClients()); });` 客户端在帖子中配置,但它没有加载客户端。它正在创建一些称为我的项目的客户端(但以后仍然不可用)
【解决方案2】:

我在 IdentityServer 4 中的 资源所有者密码验证 流程中遇到了 unauthorized_client 错误,因为在关联的 client_id 中,[dbo].[ClientGrantTypes] 中缺少授权类型 password。 我必须在表中插入一个新行来修复这个错误。

INSERT INTO [dbo].[ClientGrantTypes] (
     [GrantType]
    ,[ClientId])
VALUES ('password', X) --where X is value of [dbo].[Clients].[Id] of used client

【讨论】:

    猜你喜欢
    • 2016-10-06
    • 2018-07-19
    • 2019-07-25
    • 1970-01-01
    • 2017-02-08
    • 2013-08-03
    • 2015-02-01
    • 2021-03-06
    • 2018-07-07
    相关资源
    最近更新 更多