承载错误=“ invalid_token”,错误_说明=“发行人无效”

时间:2020-02-19 17:35:25

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

我有一个简单的Web api项目,如下所示:

[Authorize]
        [Route("Get")]
        public ActionResult<string> SayHello()
        {
            return "Hello World";
        }

我正在尝试与邮递员进行测试。请按照以下步骤操作:https://kevinchalet.com/2016/07/13/creating-your-own-openid-connect-server-with-asos-testing-your-authorization-server-with-postman/

1)在下面发送请求并收到预期的令牌:

enter image description here

2)尝试发送另一个带有授权令牌的请求,如下所示:

enter image description here

为什么会出现401(未经授权)错误? WWW-Authenticate响应标头显示:Bearer error =“ invalid_token”,error_description =“发行人无效”。我正在使用.Net Core 3.1。我已在屏幕快照中注释掉了敏感信息。

从MVC应用程序访问时,Web api可以按预期工作。

这是启动代码:

services.AddAuthentication("Bearer")
                .AddIdentityServerAuthentication(options =>
                {
                    options.Authority = identityUrl; //identityurl is a config item
                    options.RequireHttpsMetadata = false;
                    options.ApiName = apiName;

                });

3 个答案:

答案 0 :(得分:3)

我遇到了类似的问题。在发送请求并使用外部IP访问在Kubernetes集群内部运行的Keycloak实例时,我是通过Postman生成令牌的。当我在集群中的服务尝试根据权限验证令牌时,它失败了,因为用于验证令牌的内部服务名称(http:// keycloak)与Postman用来生成令牌的内部服务名称(

由于这只是为了测试,因此我将ValidateIssuer设置为false。

options.TokenValidationParameters = new TokenValidationParameters
{
    ValidateIssuer = false 
};

答案 1 :(得分:3)

我在 dotnet 5.0 上,将 swagger (NSwag.AspNetCore) 添加到我的 AzureAD“受保护”web api 并收到关于无效发行者的类似错误:

 date: Tue,16 Mar 2021 22:50:58 GMT 
 server: Microsoft-IIS/10.0 
 www-authenticate: Bearer error="invalid_token",error_description="The issuer 'https://sts.windows.net/<your-tenant-id>/' is invalid" 
 x-powered-by: ASP.NET 

所以,我没有验证发行者,而是将 sts.windows.net 添加到列表中(最后是重要部分):

// Enable JWT Bearer Authentication
services.AddAuthentication(sharedOptions =>
{
    sharedOptions.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
    Configuration.Bind("AzureAd", options);
    // Authority will be Your AzureAd Instance and Tenant Id
    options.Authority = $"{Configuration["AzureAd:Instance"]}{Configuration["AzureAd:TenantId"]}/v2.0";

    // The valid audiences are both the Client ID(options.Audience) and api://{ClientID}
    options.TokenValidationParameters.ValidAudiences = new[]
    {
        Configuration["AzureAd:ClientId"], $"api://{Configuration["AzureAd:ClientId"]}",

    };
    // Valid issuers here:
    options.TokenValidationParameters.ValidIssuers = new[]
    {
        $"https://sts.windows.net/{Configuration["AzureAd:TenantId"]}/",
        $"{Configuration["AzureAd:Instance"]}{Configuration["AzureAd:TenantId"]}/"
    };
});

这解决了我的问题。现在,为什么 NSwag 使用 sts.windows.net 作为令牌发行者,我不知道。好像不对。我正在使用这些软件包版本:

    <PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.3" />
    <PackageReference Include="NSwag.AspNetCore" Version="13.10.8" />

答案 2 :(得分:1)

AddIdentityServerAuthentication中间件的{{1}}应该是您的身份服务器的基址,中间件将联系身份服务器的OIDC元数据端点以获取用于验证JWT令牌的公共密钥。

请确认授权机构是您颁发jwt令牌的身份服务器的URL。