c# - 承载错误 ="invalid_token", error_description ="The issuer is invalid"

标签 c# asp.net-core .net-core postman identityserver4

我有一个简单的 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 :

enter image description here

2) 尝试使用授权 token 发送另一个请求,如下所示:

enter image description here

为什么我会收到 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/

相关文章:

c# - 使 span 元素在中继器的 ItemBound 事件中可见

c# - 在stackexchange redis中存储ConnectionMultiplexer的正确位置在哪里

c# - asp.net c# SqlDataSource超时问题

c# - asp.net core项目发布后iis立即关闭

c# - asp net core app出现MSB3277怎么办

visual-studio - 无法在具有慢速(玻利维亚)互联网的 Visual Studio for Mac 和 dotnet 核心中恢复 nuget 包

c# - 编码(marshal) Win32 结构时的安全句柄 (PROCESS_INFORMATION)

c# - 部署到 Azure 应用服务后找不到我的存储库文件

c# - 如何更改 Visual Studio Code 中的默认构建输出目录

asp.net-mvc - IdentityServer4 用户管理与单独的 MVC 客户端 (AspNetIdentity)