无效的令牌-观众“空”无效

问题描述

我在ASP.NET Core 3.1项目上具有以下Identity Server 4配置:

services
  .AddIdentityServer(y => {
    y.Events.raiseerrorEvents = true;
    y.Events.Raise@R_63_4045@ionEvents = true;
    y.Events.RaiseFailureEvents = true;
    y.Events.RaiseSuccessEvents = true;
  })
  .AddDeveloperSigningCredential()
  .AddInMemoryPersistedGrants()
  .AddInMemoryIdentityResources(Config.IdentityResources())
  .AddInMemoryApiResources(Config.ApiResources())
  .AddInMemoryApiScopes(Config.GetApiScopes())
  .AddInMemoryClients(Config.Clients)
  .AddProfileService<ProfileService>()
  .AddAspNetIdentity<User>();

Config如下:

public static class Config {

  public static List<ApiResource> ApiResources() {
    return new List<ApiResource> { 
      new ApiResource("api","API Resource")
    };
  }

  public static List<ApiScope> ApiScopes() {
    return new List<ApiScope> { 
      new ApiScope("api","API Scope") 
    };
  }

  public static List<IdentityResource> IdentityResources() {
    return new List<IdentityResource> { 
      new IdentityResources.OpenId(),new IdentityResources.Profile(),new IdentityResources.Email() 
    };
  }   

  public static List<Client> Clients() {
    return new List<Client> { 
      new Client {
        ClientId = "mvc",ClientName = "MVC Client",AllowedGrantTypes = GrantTypes.ClientCredentials,ClientSecrets = { new Secret("Secret".Sha256()) }
        AllowedScopes = { 
          IdentityServerConstants.StandardScopes.OpenId,IdentityServerConstants.StandardScopes.Profile,IdentityServerConstants.StandardScopes.Email,IdentityServerConstants.StandardScopes.OfflineAccess,"api"
        }         
      }
    };
  }

}

在API上,我有

  services
    .AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
    .AddIdentityServerAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme,x => {
      x.ApiName = "api";
      x.ApiSecret = "Secret"
      x.Authority = Config.AuthorityUrl;
      x.RequireHttpsMetadata = true;
      x.EnableCaching = true;
      x.CacheDuration = TimeSpan.FromMinutes(20);
    });

我使用带有OAuth2的Insomnia客户端调用一个API端点:

enter image description here

我能够获得访问令牌,但是在调用API时出现错误

Bearer error="invalid_token",error_description="The audience 'empty' is invalid"

我使用JWT检查了访问令牌,并且得到了以下信息:

标题

{
  "alg": "HS256","kid": "A1042705E52832C676596F36BB1898AB","typ": "at+jwt"
}

有效载荷

{
  "nbf": 1598478410,"exp": 1598482010,"iss": "https://localhost:5000","client_id": "mvc","jti": "23525FF997FD4D71E13C786A7AD07B5D","iat": 1598478410,"scope": [
    "api"
  ]
}

aud丢失。但是我需要吗?阅读IS4文档后,我尝试添加

.AddIdentityServer(y => {
  y.EmitStaticAudienceClaim = true;

现在,访问令牌具有aud字段,但其值不是api。不是吗?

"aud": "https://localhost:5000/resources"

调用API时,我现在收到错误消息:

Bearer error="invalid_token",error_description="The audience 'https://localhost:5000/resources' is invalid"

我阅读了文档和IS4示例,但找不到解决方案。

我想念什么?

解决方法

您需要做的是将ApiScope与APIRespource连接起来,以使api成为所需的作用域。当范围和api未“连接”时,https:// localhost:5000 / resources aud声明是普通读者。

在您的API定义中,您需要执行以下示例中的操作(请看下面的Scopes属性)

var invoiceApi = new ApiResource()
{
    Name = "invoice",//This is the name of the API
    Description = "This is the invoice Api-resource description",Enabled = true,DisplayName = "Invoice API Service",Scopes = new List<string> { "shop.admin","shop.employee" },};

有关使用的通用资源范围的详细信息,请参见此link

它说:

使用仅合并范围的模型时,不会添加任何音频(受众)声明 令牌,因为此概念不适用。如果您需要声音 声明,您可以在选项上启用EmitStaticAudience设置。 这将发出issuer_name / resources格式的音频声明。如果 您需要对aud声明进行更多控制,请使用API​​资源。

您可以尝试将 EmitStaticAudience 选项设置为 false

您还可以忽略令牌中的两个受众。