C# 承载错误=“;无效的“U令牌”;,错误描述=";“发行人无效”;
我有一个简单的web api项目,如下所示:C# 承载错误=“;无效的“U令牌”;,错误描述=";“发行人无效”;,c#,asp.net-core,.net-core,postman,identityserver4,C#,Asp.net Core,.net Core,Postman,Identityserver4,我有一个简单的web api项目,如下所示: [Authorize] [Route("Get")] public ActionResult<string> SayHello() { return "Hello World"; } AddIdentityServerAuthentication中间件的权限应该是identityserver的基址,中间件将联系identity server的OIDC
[Authorize]
[Route("Get")]
public ActionResult<string> SayHello()
{
return "Hello World";
}
AddIdentityServerAuthentication
中间件的权限
应该是identityserver的基址,中间件将联系identity server的OIDC元数据端点以获取公钥以验证JWT令牌
请确认授权是您颁发jwt令牌的identity server的url。我遇到了类似问题。在发送请求并使用外部IP访问在kubernetes集群内运行的KeyClope实例时,我通过邮递员生成令牌。当集群内的我的服务尝试根据授权验证令牌时,它失败了,因为内部服务名称无效(http://keycloak)它用来验证令牌与邮递员用来生成令牌的不同(I'm on dotnet 5.0,将swagger(NSwag.AspNetCore)添加到我的AzureAD“protected”中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
这解决了我的问题。现在,我不知道为什么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" />
访问令牌在证书中。它正在失败。安全模式是TLS/SSL,它有许多不同的选项,如16位、32位、64位。我见过很多人在升级到Net 4.7时安全性失败。我怀疑Core 3.1也会出现同样的情况。因此,您正在使用的令牌和c代码中设置的模式不一样。你可能想看维基文章以更好地理解:我如何在C代码中找到模式?谢谢。我在问题的底部添加了一些C代码。请看一看?好问题。我还没有从人们那里得到任何关于如何解决此问题的真实反馈。我怀疑这与C代码有关lass和编译模式x64或x86。当microsoft发布Net 4.7时,它似乎已经崩溃。由于Core 3.1也是新版本,我怀疑Core 3.1中也存在同样的问题,您可以尝试针对旧版本的Net或编译器选项。权限是什么,它应该是您的IdentityServer的基址我也有类似的问题,但添加了以下问题:呃,请参阅我的有效发行人列表以解决问题,请参阅
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
// 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"]}/"
};
});
<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" />