授权头需要“凭证”参数

65

我们正在使用Identity Server4和.NET Core,并将应用程序部署为AWS Serverless Lambda函数。当调用令牌端点以生成访问令牌时,我们收到以下错误消息:

{
"message": "Authorization header requires 'Credential' parameter. Authorization header requires 'Signature' parameter. Authorization header requires 'SignedHeaders' parameter. Authorization header requires existence of either a 'X-Amz-Date' or a 'Date' header. Authorization=Basic Y2xpZW50OnNlY3JldA=="

}

这是我们在 Identity Server 应用程序中的 ConfigurationServices 方法:

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton<IConfiguration>(Configuration);

        //connection string
        string connectionString = Configuration.GetConnectionString("IdentityServer");

        var rsaProvider = new RSACryptoServiceProvider(2048);

        SecurityKey key = new RsaSecurityKey(rsaProvider);

        var credentials = new Microsoft.IdentityModel.Tokens.SigningCredentials
              (key, SecurityAlgorithms.RsaSha256Signature);


        var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;

        services.AddIdentityServer()
           .AddSigningCredential(credentials)
            // this adds the config data from DB (clients, resources)
            .AddConfigurationStore(options =>
            {
                options.ConfigureDbContext = builder =>
                builder.UseSqlServer(connectionString,
                sql => sql.MigrationsAssembly(migrationsAssembly));
            }) // this adds the operational data from DB (codes, tokens, consents)
            .AddOperationalStore(options =>
            {
                options.ConfigureDbContext = builder =>
                builder.UseSqlServer(connectionString,
            sql => sql.MigrationsAssembly(migrationsAssembly));

                // this enables automatic token cleanup. this is optional.
                 options.EnableTokenCleanup = true;
                 options.TokenCleanupInterval = 30;
            });

        // Add S3 to the ASP.NET Core dependency injection framework.
        services.AddAWSService<Amazon.S3.IAmazonS3>();
    }

这是我们的客户端应用程序,调用身份验证服务器的令牌终结点来生成令牌:

[HttpGet]
    public async Task<IActionResult> Get(string client, string secret)
    {

        IActionResult result = null;

        //discover endpoints from metadata

        //var disco = await DiscoveryClient.GetAsync("http://localhost:3000/");

        var disco = await DiscoveryClient.GetAsync("hide for security reasons/");

        if (disco.IsError)
        {
            result = NotFound(disco.Error);

            return result;
        }
        //request token

        var tokenClient = new TokenClient(disco.TokenEndpoint, client, secret);

        var tokenResponse = await tokenClient.RequestClientCredentialsAsync(scope: "sup");

        if (tokenResponse.IsError)
        {
            result = NotFound(tokenResponse.Error);
        }

        result = Ok(tokenResponse.Json);

        return result;
    }

你有发送的原始请求的详细信息吗? - mackie
5
嗨@mackie,问题已经解决。实际上我将lambda函数部署为GET http方法,但当我们调用令牌终端时,它实际上是POST请求。因此,当我更改了lambda函数的http方法后,它就可以工作了。 :) - Rakesh Kumar
6个回答

130

以防其他人也遇到这种情况,我在 URL 路径中有一个拼写错误导致出现了这个问题。

当我纠正了我的拼写错误后,一切都正常了。

小背景: 我感到很困惑,因为我正在为我的 API Gateway 资源使用 Lambda 授权程序,但我甚至没有看到任何东西命中该 Lambda 的 Cloudwatch 日志。


4
对我而言,这是因为在使用curl之前忘记部署。 - AlleyOOP
5
您真是个救星,先生。 - Ericson Willians
6
我也在这里寻找相同的答案。结果发现我的HTTP动词不对,而不是URL。对于我来说,当我忘记并再次谷歌时...... - MarkC
7
我的错误原因是:api.example.com/stage/endpoint 不是应该使用的 URL。相反,正确的 URL 是 api.example.com/endpoint,因为阶段已经在 API 映射中指定了。我的背景是我正在使用自定义域名作为我的 API 网关端点。 - Iching Chang
2
我也是!错过了基本路径的一部分。嘿,亚马逊云服务,下次来个简单的404怎么样?摇头。 - Matthew Snyder
显示剩余6条评论

3
我遇到的问题是,在粘贴URL时,可能包含换行符或其他不可见字符不匹配。

1
我在尝试使用curl访问端点(*)时遇到了这个错误:
curl -XGET -u user:password <host-url>

问题在于我传递了错误的凭据。

(*) 顺便说一下:我尝试搜索我在AWS上托管的Elasticsearch集群。


0
在我的情况下,我发现 AWS API Gateway 中的 URL 路径是区分大小写的。 希望这个答案能帮助像我一样陷入这个问题的人。

0
我最近遇到了同样的问题!我配置了一个Lambda函数,并将其连接到API Gateway中的一个路由(资源)。然而,由于我的请求有一个名为“Authorization”的头参数,当我尝试访问API Gateway生成的路由(api.gateway.UrlExample.com/stage/endpoint)时,我遇到了问题(“Authorization header requires 'Credential' parameter”)。
以下是我解决问题的方法: 在AWS控制台中,进入API Gateway的资源界面:
- 选择导致问题的资源。 - 转到“Integration Request”选项卡。 - 点击“编辑”。 - 启用“Lambda Proxy Integration”的复选框,然后保存。
等待几分钟,然后再次尝试发送请求!

-1
如果您正在使用Postman访问API Gateway端点,可能会在Postman中遇到此错误。特别是当您尝试传递id令牌或访问令牌时,它会发生。
为了解决这个问题,您需要使用AWS-Amplify对请求进行签名。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接