【问题标题】:Identity Server 4 Client Credentials for custom endpoint on token Server令牌服务器上自定义端点的 Identity Server 4 客户端凭据
【发布时间】:2019-03-26 14:09:31
【问题描述】:

我使用 Identity Server 4 实现了一个令牌服务器。

我向令牌服务器添加了一个自定义 API 端点,并在身份验证方面遇到了困难。自定义端点继承自 ControllerBase,具有 3 种方法(GET、POST、DELETE)。

我打算使用具有凭据(服务器到服务器)的专用客户端在另一个 API 中调用自定义端点,该客户端在 .NET Core 中实现为 HttpClient。没有用户参与其中。

为了获取访问令牌,我使用了 IdentityModel DiscoveryClient 和 TokenEndpoint。

所以到目前为止我做了以下事情:

  1. 设置“常规”身份服务器并验证它是否有效 -> 有效
  2. 实现自定义端点并在未经授权的情况下对其进行测试 -> 可以正常工作
  3. 添加另一个具有自定义范围“api.auth.endpoint1”的 api 资源(“api.auth”)
  4. 使用允许访问范围“api.auth.endpoint1”的客户端凭据设置客户端。
  5. 实现 HttpClient 和测试设置 -> 我通过身份模型令牌端点获得访问令牌。

现在,当我使用收到的访问令牌使用 HttpClient 调用端点时,我得到响应代码 200(OK),但内容是身份服务器的登录页面。

documentation of Identity Server 4状态使用

   services.AddAuthentication()
    .AddIdentityServerAuthentication("token", isAuth =>
    {
        isAuth.Authority = "base_address_of_identityserver";
        isAuth.ApiName = "name_of_api";
    });

以及使用

   [Authorize(AuthenticationSchemes = "token")]

不幸的是,编译器指出 .AddIdentityServerAuthentication 无法找到。我错过了一个特殊的 nuget 吗?

到目前为止,我在令牌服务器上使用的 nuget 是:

  • IdentityServer4 (v2.2.0)
  • IdentityServer4.AspNetIdentity (v2.1.0)
  • IdentityServer4.EntityFramework (v2.1.1)

想通了那部分。 AddIdentityServerAuthentication 缺少的 nuget 是:

  • IdentityServer4.AccessTokenValidation

在自定义范围的授权上苦苦挣扎。

有人知道如何配置安全性吗?

【问题讨论】:

    标签: asp.net-core oauth-2.0 .net-core identityserver4


    【解决方案1】:

    使用 ClientGrantTypes = client_credentials 和您的 api 配置客户端,如下所示:

    services
        .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
        .AddIdentityServerAuthentication(options =>
        {
            options.Authority = "http://localhost:5000";
            options.ApiName = "api.auth";
        });
    

    其中 ApiName 是资源的名称。请注意 resource != scope。在大多数示例中,资源名称等于作用域名称。但在您的情况下,资源名称是 api.auth 而范围名称是 api.auth.endpoint1

    配置客户端请求范围

    var tokenClient = new TokenClient(disco.TokenEndpoint, clientId, secret);
    var tokenResponse = await tokenClient.RequestClientCredentialsAsync("api.auth.endpoint1");
    

    IdentityServer 将查找资源名称并将其作为受众 (aud) 添加到令牌中,同时将范围添加为具有类型范围的声明。

    这应该足以让它工作。还要检查sample project

    【讨论】:

      【解决方案2】:

      针对捆绑在一起的不同访问权限的自定义身份验证方案和基于范围的策略如下所示:

      // Startup.ConfigureServices
      services
        .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
        .AddIdentityServerAuthentication("CustomAuthEndpointsAuthenticationScheme", options =>
        {
          options.Authority = "http://localhost:5000";
          options.ApiName = "api.auth"; //IdentityServer4.Models.ApiResource.Name aka Audience
        });
      
      services.AddAuthorization(options => 
      {
        options.AddPolicy("Endpoint1Policy", policy => {
          policy.AddAuthenticationSchemes(new string[] { "CustomAuthEndpointsAuthenticationScheme" });
          policy.RequireScope("api.auth.endpoint1"); } ); //IdentityServer4.Models.Scope.Name
        options.AddPolicy("Endpoint2Policy", policy => {
          policy.AddAuthenticationSchemes(new string[] { "CustomAuthEndpointsAuthenticationScheme" });
          policy.RequireScope("api.auth.endpoint2"); } ); //IdentityServer4.Models.Scope.Name
      } );
      
      // securing the custom endpoint controllers with different access rights
      [Authorize(AuthenticationSchemes = "CustomAuthEndpointsAuthenticationScheme", Policy = "Endpoint1Policy")] 
      

      它似乎不会干扰 IdentityServer4 默认端点,也不会干扰 ASP.NET Core Identity 部分。

      【讨论】:

        猜你喜欢
        • 2022-08-17
        • 2018-07-06
        • 2017-08-12
        • 2019-01-03
        • 1970-01-01
        • 2020-07-19
        • 2022-07-14
        • 2019-07-25
        • 1970-01-01
        相关资源
        最近更新 更多