使用grant_type:authorization_code时,Identity Server 4 / connect / token返回400错误请求

问题描述

我已经安装了Identity Server 4应用程序。尝试设置角度客户端应用程序。 在获得授权代码后,有角应用调用/connect/token api,即可访问提供商登录页面并成功登录,并获得400错误请求。经过Postman的测试,使用时可以正常工作

grant_type:client_credentials

信息

https:// localhost:5001 / connect / token

grant_type:authorization_code
client_id:angular_app_client_id
code_verifier:C0.731426161751518815973254dfd6715973254680670.9347059398232558
code:oSEydAywPs_zg4MRwbqwH-ly8ToWdrtexTDgx0ee7eY
redirect_uri:http://localhost:4200/callback
client_secret:<secret>
require_pkce:false
requried_client_secret:false

Angular应用中的OpenIdConfiguration

const openIdConfiguration: OpenIdConfiguration = {
    stsServer: this.authUrl,redirect_url: this.originUrl + 'callback',client_id: 'demo_client_id',response_type: 'code',scope: 'openid profile',post_logout_redirect_uri: this.originUrl,forbidden_route: '/forbidden',unauthorized_route: '/unauthorized',silent_renew: true,silent_renew_url: this.originUrl + '/silent-renew.html',history_cleanup_off: true,auto_userinfo: true,log_console_warning_active: true,log_console_debug_active: true,max_id_token_iat_offset_allowed_in_seconds: 10,};

邮递员请求

enter image description here

服务器日志

{"ClientId": "angular_app_client_id","Category": "Authentication","Name": "Client Authentication Failure","EventType": "Failure","Id": 1011,"Message": "Invalid client secret","ActivityId": "0HM1VK1OS0MDK:00000001","TimeStamp": "2020-08-13T16:29:52.0000000Z","ProcessId": 2002,"LocalIpAddress": "::1:5001","RemoteIpAddress": "::1","$type": "ClientAuthenticationFailureEvent"}

当grant_type为client_credentials时工作

enter image description here

请帮助...

解决方法

  • 您的角度客户端应用程序是一个交互式应用程序,因此您需要将RequireClientSecret设置为false。
  • 从客户端应用中删除client_secret并删除对IdentityServer的任何调用
  • 此外,您还需要为交互式应用程序使用代码授予而不是客户端授权授予
    • 此更改之后,数据库中的客户端配置应如下所示:
client_id = angular_app_client_id,client_name = Angular App,grant_type = authorization_code
  • 请注意,我删除了秘密,也删除了client_credentials,因为这笔赠款不可用于水疗应用

  • 将(RequirePkce)[https://github.com/IdentityServer/IdentityServer4/blob/main/src/EntityFramework.Storage/src/Entities/Client.cs#L29]设置为true客户端配置-您在数据库中的情况

  • 在js客户端上
  • 设置response_type: "code"

    • 以下是您的js客户端代码应如何显示的示例
var config = {
    authority: "http://localhost:5000",client_id: "jsclient",redirect_uri: "http://localhost:5003/callback.html",response_type: "code",scope:"openid profile",post_logout_redirect_uri : "http://localhost:5003/index.html",};
  • 您还需要将SPA应用程序的AllowedCorsOrigins设置为应用程序URL。这是因为它是从浏览器中调用的。

我有一个完整的工作示例here,随时查看

详细了解如何设置交互式应用Here

,

要使用授权码流获取令牌,您需要传递以下详细信息:

        client_id = "authcodeflowclient",//Id of this client
        client_secret = "mysecret",grant_type = "authorization_code",code_verifier = code_verifier,code = code,redirect_uri = "https://localhost:5001/CodeFlow/callback"

设置秘密后,您需要使用

对其进行哈希处理
            ClientSecrets = new List<Secret>
            {
                new Secret
                {
                    Value = "mysecret".Sha512()
                }
            },

在使用授权码流时,应确保将其设置为

AllowedGrantTypes = GrantTypes.Code,

有帮助吗?