我有一个简单的 web api 项目,它看起来像这样:
[Authorize]
[Route("Get")]
public ActionResult<string> SayHello()
{
return "Hello World";
}
我正在尝试使用 Postman 对其进行测试。按照此处的步骤操作:https://kevinchalet.com/2016/07/13/creating-your-own-openid-connect-server-with-asos-testing-your-authorization-server-with-postman/
1)发送下面的请求并按预期接收 token :
2) 尝试使用授权 token 发送另一个请求,如下所示:
为什么我会收到 401(未经授权)错误? WWW-Authenticate 响应 header 表示: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;
});
最佳答案
我遇到了类似的问题。在发送我的请求并使用外部 IP 访问在我的 kubernetes 集群内运行的 Keycloak 实例时,我通过 Postman 生成了我的 token 。当我在集群内的服务尝试根据权限验证 token 时,它失败了,因为它用于验证 token 的内部服务名称 (http://keycloak) 与 Postman 用于生成 token 的名称不同 (ValidateIssuer
为假。options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false
};
关于c# - 承载错误 ="invalid_token", error_description ="The issuer is invalid",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60306175/