【问题标题】:Thinktecture Identity Server 3 Asp.Net Identity Sample, constantly getting 401 authorization deniedThinktecture Identity Server 3 Asp.Net Identity Sample,不断收到 401 授权被拒绝
【发布时间】:2017-11-21 00:30:01
【问题描述】:

我正在尝试让身份服务器的 Asp.Net 身份示例在我的 Web Api 项目中工作,而初学者我正在尝试使用不记名令牌获取令牌并向 API 发送请求。

所以使用提琴手,我可以向https://localhost:44333/core/connect/token 发送一个请求,内容如下:client_id=roclient&client_secret=secret&grant_type=password&username=bob&password=mypass&scope=read write offline_access,它工作正常,我得到了访问令牌和刷新令牌。

然后我的解决方案中有一个 Web Api 项目,其中包含使用不记名令牌的代码:

app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
        {
            Authority = "https://localhost:44333",
            ValidationMode = ValidationMode.ValidationEndpoint,

            RequiredScopes = new[] { "api1" }
        });

以及一个使用[Authorize]装饰器保护api的测试控制器。

正如我所说,获取令牌工作正常,但是当我使用提琴手将请求发送到 api 时,我总是得到“401 授权被拒绝......”的事情。

我在提琴手请求中提出的当然是:

Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6ImE...

我做错了吗?

【问题讨论】:

    标签: asp.net-identity asp.net-web-api2 thinktecture-ident-server identityserver3 thinktecture


    【解决方案1】:

    在您的令牌请求中,您正在请求读取和写入范围。您的 api 检查 api1 范围。这行不通。

    【讨论】:

    • 我将令牌请求更改为包括“读取”、“写入”和“离线访问”范围。如何使 API 控制器中的方法成为这些范围的一部分?因为我仍然得到 401。
    • 我非常感谢更详细的解释,我很难使用文档从头开始理解 IdentityServer :(
    • 您的 api 中需要一个名为“api1”的范围。令牌(至少从您的请求中可以看出)不包括该范围。
    【解决方案2】:

    给你,我已经修改了你的示例,所以它对你有用:

    app.UseIdentityServerBearerTokenAuthentication(new 
    IdentityServerBearerTokenAuthenticationOptions
    {
        Authority = "https://localhost:44333",
        ValidationMode = ValidationMode.ValidationEndpoint,
    
        RequiredScopes = new[] { "read", "write" } // scopes
    });
    

    【讨论】:

      猜你喜欢
      • 2020-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-31
      • 2015-02-12
      • 2015-05-29
      • 2015-11-14
      相关资源
      最近更新 更多