首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无效令牌-观众“空”无效

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

Stack Overflow用户
提问于 2020-08-26 21:56:49
回答 1查看 9.2K关注 0票数 5

我在ASP.NET Core3.1项目上有以下身份服务器4配置:

代码语言:javascript
复制
services
  .AddIdentityServer(y => {
    y.Events.RaiseErrorEvents = true;
    y.Events.RaiseInformationEvents = 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如下:

代码语言:javascript
复制
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上:

代码语言:javascript
复制
  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使用调用了一个API端点:

我能够获得访问令牌,但是当调用API时,我得到了错误:

代码语言:javascript
复制
Bearer error="invalid_token", error_description="The audience 'empty' is invalid"

我使用JWT检查了访问令牌,得到了以下内容:

报头

代码语言:javascript
复制
{
  "alg": "HS256",
  "kid": "A1042705E52832C676596F36BB1898AB",
  "typ": "at+jwt"
}

有效载荷

代码语言:javascript
复制
{
  "nbf": 1598478410,
  "exp": 1598482010,
  "iss": "https://localhost:5000",
  "client_id": "mvc",
  "jti": "23525FF997FD4D71E13C786A7AD07B5D",
  "iat": 1598478410,
  "scope": [
    "api"
  ]
}

aud失踪了。但我需要它吗?在阅读了IS4文档之后,我尝试添加:

代码语言:javascript
复制
.AddIdentityServer(y => {
  y.EmitStaticAudienceClaim = true;

现在访问令牌有aud字段,但它的值不是api。应该是吧?

代码语言:javascript
复制
"aud": "https://localhost:5000/resources"

当调用API时,我现在得到了错误:

代码语言:javascript
复制
Bearer error="invalid_token", error_description="The audience 'https://localhost:5000/resources' is invalid"

我读过docs和IS4示例,但找不到解决方案。

我遗漏了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-27 06:52:18

您需要做的是将ApiScope与APIRespource连接起来,使其成为所需的范围。当作用域和api没有“连接”时,https://localhost:5000/resources aud声明是一个通用的受众。

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

代码语言:javascript
复制
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" },
};

有关使用的通用资源范围的详细信息,请参阅此链接

上面写着:

当使用只有作用域的模型时,不会将aud (受众)声明添加到令牌中,因为这个概念不适用。如果您需要一个aud声明,您可以在选项上启用EmitStaticAudience设置。这将以发行者名称/资源格式发出一个aud声明。如果您需要对aud索赔进行更多控制,请使用API资源。

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

您也可以忽略令牌中的两个观众。

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63606288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档