Identity Server 4-未经授权的客户端

时间:2019-09-09 16:23:03

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

我正在为带有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())   ;

然后我在邮递员中尝试:enter image description here 并总是得到:

{"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中定义的客户端“一起使用”,但只能与根据文档定义的预定义模板一起使用,但是无法(或未记录这种可能性)禁用PKCE以使其正常工作。与邮递员等。

1 个答案:

答案 0 :(得分:0)

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

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

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

  • 重定向URI:这将强制将响应代码令牌重定向到您应该作为客户端的先前已知地址(除非使用主机文件来替换您的网站)

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