【问题标题】:Trouble authorizing .NET 5 Web API reference tokens to IdentityServer3 using IdentityServer4.AccessTokenValidation package使用 IdentityServer4.AccessTokenValidation 包向 IdentityServer3 授权 .NET 5 Web API 引用令牌时遇到问题
【发布时间】:2021-12-03 12:01:59
【问题描述】:

服务器

使用 IdentityServer3 进行客户端/应用程序授权。

使用 IdentityAdmin 通过 GUI 编辑客户端/范围。

为 API 创建了一个新客户端,添加了 SharedSecret 和 api 范围。

API / 客户端

有 2 个 GET 端点。

使用 IdentityServer4.AccessTokenValidation NuGet 包。

配置应该很简单:

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers(c => {
        var policy = ScopePolicy.Create("api");
        c.Filters.Add(new AuthorizeFilter(policy));
    });

    services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
        .AddIdentityServerAuthentication(options => {
            options.Authority = "{base url of identity server}";
            options.ApiName = ""; // not sure what this is? client id from identity server?
            options.ApiSecret = ""; // should this be the hashed password?
            options.LegacyAudienceValidation = true;
        });

    services.AddSwaggerGen(c => {
        c.SwaggerDoc("v1", new OpenApiInfo { Title = "MarvalAPI", Version = "v1" });
    });

    RegisterServices(services);
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment()) {
        app.UseDeveloperExceptionPage();
        app.UseSwagger();
        app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "MarvalAPI v1"));
    }

    app.UseHttpsRedirection();

    app.UseRouting();

    app.UseAuthentication(); //this is added, everything else is by default
    app.UseAuthorization();

    app.UseEndpoints(endpoints => {
        endpoints.MapControllers();
    });
}

测试:

  1. 从身份“/connect/token”端点获取客户端引用令牌
  2. GET API 的端点添加了标头“Authorization: Bearer {token}”
  3. 收到 401 Unauthorized

我尝试过的事情:

  • 不同的 Startup.cs 配置
  • 尝试通过身份“/connect/accesstokenvalidation”端点验证令牌,令牌有效
  • 不同的 apiname/apisecret 值,因为不能 100% 确定它们必须是什么。
  • 谷歌搜索无济于事

我在这里不知所措,我做错了什么吗?这只是一个兼容性问题吗?还是我根本什么都不懂?似乎缺乏清晰的文档,用户必须提取信息。

使用的来源

https://github.com/IdentityServer/CrossVersionIntegrationTests/blob/main/src/CoreApiIdSrv3/Startup.cs

https://github.com/IdentityServer/IdentityServer4.AccessTokenValidation

IdentityServer3 文档

SO / github/identityserver3 个线程。

【问题讨论】:

    标签: c# .net access-token .net-5 identityserver3


    【解决方案1】:

    好吧,在发完这篇文章后的一段时间,我想通了。

    options.ApiName = "";
    options.ApiSecret = "";
    

    ApiName 是客户端使用的作用域 的名称,因此在这种情况下,该值应为 api

    ApiSecret范围密钥PRE-HASHED 值。

    例如 如果秘密值是“test”并且它的 SHA256 值是 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08,那么 ApiSecret 值应该是 test

    所以,在弄清楚这一点之后,上面的选项配置应该是这样的:

    options.ApiName = "api";
    options.ApiSecret = "test";
    

    注意: SHA512 也可以。

    对我来说,这似乎是一个主要的命名问题。

    我在分析了这个 VS 解决方案后解决了这个问题:

    https://github.com/IdentityServer/CrossVersionIntegrationTests

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-16
      • 1970-01-01
      • 1970-01-01
      • 2021-11-06
      • 2018-07-26
      • 2015-04-24
      • 2019-04-27
      • 2021-12-04
      相关资源
      最近更新 更多