首页 > 解决方案 > 带有 ASP.NET WEB API 2 的 IdenityServer3 给出授权被拒绝消息

问题描述

作为 API 验证的一部分,我们使用IdenityServer4 NuGet 包和网络核心创建了一个身份服务器。我们现有的 API 在ASP.NET WEB API 2中运行。由于 IdenityServer4 NuGet 包不能与 WEB API 2 压缩,我们使用IdenityServer3 NuGet 包来验证令牌。我们能够从 Idenityserver 获取令牌,但是当我们将此令牌传递给 API(在标头中)时,它会显示 - “此请求的授权已被拒绝。” . 我在这里做错了什么?

启动类(在 WEB API 中)

[assembly: OwinStartup(typeof(WebApplication2.Startup))]
namespace WebApplication2
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
            {
                Authority = "https://localhost:44385/",
                RequiredScopes = new[] { "offline_access openid api/Default" }
            });
        }
    }
}

URL - https://localhost:44385/是我的 IdenityServer 运行的地方。

我的 Controller 类如下所示

[Authorize]
public class DefaultController : ApiController
{
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
}

我的 Postman 请求关于我如何调用我的 API 的屏幕截图

在此处输入图像描述

标签: c#asp.net-web-api2identityserver4identityserver3

解决方案


推荐阅读