JWT令牌.NET核心身份服务器问题
我正在尝试使用OAuth客户端凭据流保护.NET5.0WebAPI 我的客户端正在从IdentityServer4实例请求令牌,并将其提供给API。然后,当我访问端点时,API返回401错误。我注意到以下标题:JWT令牌.NET核心身份服务器问题,jwt,identityserver4,Jwt,Identityserver4,我正在尝试使用OAuth客户端凭据流保护.NET5.0WebAPI 我的客户端正在从IdentityServer4实例请求令牌,并将其提供给API。然后,当我访问端点时,API返回401错误。我注意到以下标题: WWW-Authenticate头包含承载错误=\“无效\u令牌\”,错误\u描述=\“访问群体“空”无效\” 这表明我的JWT不包含受众参数 我的JWT请求代码如下所示: var tokenResponseType = await serverClient.RequestClientC
WWW-Authenticate
头包含承载错误=\“无效\u令牌\”,错误\u描述=\“访问群体“空”无效\”
这表明我的JWT不包含受众参数
我的JWT请求代码如下所示:
var tokenResponseType = await serverClient.RequestClientCredentialsTokenAsync(new
ClientCredentialsTokenRequest
{
Address = discoveryDocument.TokenEndpoint,
ClientId = "client_id",
ClientSecret = "client_secret",
Scope = "ApiOne",
});
验证令牌的代码如下所示:
services.AddAuthentication("Bearer")
.AddJwtBearer("Bearer", config =>
{
config.Authority = "https://localhost:44335/";
config.Audience = "ApiOne";
config.TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuer = true,
ValidateActor = true,
ValidateLifetime = true,
ValidateAudience = true,
ValidateIssuerSigningKey = true
};
config.RequireHttpsMetadata = false;
});
我认为JWT令牌应该包含受众参数。当我请求JWT时,我找不到一种方法来设置访问群体参数
我已经使用jwt.io调试了我的jwt令牌,这确认了未设置访问群体值。我希望在请求上设置作用域可以做到这一点。缺少的是IdentityServer中的ApiScope和ApiResource配置 首先,您需要定义一个ApiScope,如:
new ApiScope(name: "ApiOneScope",
displayName:"You can manage the ApiOne system.",
userClaims: new List<string>{ });
新的ApiScope(名称:“ApiOneScope”,
displayName:“您可以管理ApiOne系统。”,
用户声明:新列表{});
ApiScope是客户端可以请求访问的范围
然后您需要一个ApiResource定义如下:
new ApiResource()
{
Name = "ApiOne",
DisplayName = "Orders API Service",
Scopes = new List<string> { "ApiOneScope" },
};
newapiresource()
{
Name=“ApiOne”,
DisplayName=“订单API服务”,
Scopes=新列表{“ApiOneScope”},
};
ApiResource是实际的Api,当客户端请求名为ApionScope的范围时,它最终会出现在访问群体声明中