【问题标题】:Always getting "invalid_client" from Identity Server总是从 Identity Server 获取“invalid_client”
【发布时间】:2020-07-14 18:00:03
【问题描述】:

一切似乎正常,但它不起作用,它返回“Invalid_Client” - (400 - 错误请求)。 下面两边都这么简单;

身份服务器代码:

                new Client
                {
                    ClientId = "js",
                    ClientSecrets = {
                        new Secret("secret".Sha256())
                    },
                    AllowedGrantTypes = GrantTypes.ClientCredentials,
                    RequireClientSecret = false,
                    AllowedScopes =
                    {
                        "api1"
                    }
                }

Javascript 客户端代码:

axios.post('http://localhost:5000/connect/token',request, {
        headers: {
             'client_id' : 'js',
             'client_secret' : 'secret',
             'grant_type': 'client_credentials',
             'scope' : 'api1'

             }});

【问题讨论】:

    标签: asp.net-core oauth-2.0 jwt identityserver4 access-token


    【解决方案1】:

    参数应该在请求体而不是请求头中传递,您可以将客户端脚本修改为:

    const params = new URLSearchParams();
    params.append('client_id', 'js');
    params.append('client_secret', 'secret');
    params.append('grant_type', 'client_credentials');
    params.append('scope', 'api1');
    
    axios.post('http://localhost:5000/connect/token', params, {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        }
    }).then(function (response) {
        console.log(response.data);
    
    });
    

    另外配置CORS是在客户端配置上使用AllowedCorsOrigins集合:

    new Client
    {
        ClientId = "js",
        ClientSecrets = {
            new Secret("secret".Sha256())
        },
        AllowedCorsOrigins= new List<string>() { "http://localhost:5002" },
        AllowedGrantTypes = GrantTypes.ClientCredentials,
        RequireClientSecret = false,
        AllowedScopes =
        {
            "api1"
        }
    },
    

    【讨论】:

      【解决方案2】:

      不应使用 normall fetch 或轴。

      因为该服务是 Oauth 服务,应该使用 'client-oauth2' 库。

      代码示例:

           var ClientOAuth2 = require("client-oauth2");
           var authRequest = new ClientOAuth2({
           clientId: IDENTITY_CONFIG.client_id,
           clientSecret: IDENTITY_CONFIG.client_secret,
           accessTokenUri: IDENTITY_CONFIG.token_endpoint,
           scopes: [IDENTITY_CONFIG.grantType]
        });
      
        return authRequest.credentials.getToken();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-06-15
        • 2017-04-24
        • 2011-04-11
        • 2020-01-06
        • 1970-01-01
        • 2018-08-20
        • 1970-01-01
        相关资源
        最近更新 更多