始终从 Identity Server 获取 "invalid_client"

Always getting "invalid_client" from Identity Server

一切看起来都很正常,但它不工作,它 returns “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'

             }});

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

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

不应使用正常的抓取或轴。

因为服务是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();