简体   繁体   English

使用 IdentityServer4.AccessTokenValidation 包将 .NET 5 Web API 参考令牌授权给 IdentityServer3 时遇到问题

[英]Trouble authorizing .NET 5 Web API reference tokens to IdentityServer3 using IdentityServer4.AccessTokenValidation package

Server服务器

Using IdentityServer3 for client/application authorization.使用 IdentityServer3 进行客户端/应用程序授权。

Using IdentityAdmin to edit clients/scopes via GUI.使用 IdentityAdmin 通过 GUI 编辑客户端/范围。

Created a new Client for the API, added a SharedSecret and api scope.为 API 创建了一个新客户端,添加了 SharedSecret 和 api 范围。

API / Client API / 客户端

Has 2 GET endpoints.有 2 个 GET 端点。

Uses the IdentityServer4.AccessTokenValidation NuGet package.使用 IdentityServer4.AccessTokenValidation NuGet 包。

Configuration should be simple:配置应该很简单:

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();
    });
}

Testing:测试:

  1. GET client reference token from identity "/connect/token" endpoint从身份“/connect/token”端点获取客户端引用令牌
  2. GET API's endpoint with added header "Authorization: Bearer {token}" GET API 的端点,添加了标头“Authorization: Bearer {token}”
  3. Receive 401 Unauthorized收到 401 未经授权

Things I have tried:我尝试过的事情:

  • Different Startup.cs configurations不同的 Startup.cs 配置
  • Tried validating token via identity "/connect/accesstokenvalidation" endpoint, token is valid .尝试通过身份“/connect/accesstokenvalidation”端点验证令牌,令牌有效
  • Different apiname/apisecret values, because not 100% sure what they have to be.不同的 apiname/apisecret 值,因为不是 100% 确定它们必须是什么。
  • Googled to no avail谷歌搜索无果

I am at a loss here, am I doing something totally wrong?我在这里不知所措,我做错了什么吗? Is this just a compatibility issue?这只是兼容性问题吗? Or am I just not understanding anything at all?还是我根本就什么都不懂? Seems like clear documentation is scarce and users have to draw out information.似乎缺乏清晰的文档,用户必须提取信息。

Sources used使用的来源

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

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

IdentityServer3 documentation IdentityServer3 文档

SO / github/identityserver3 threads. SO / github/identityserver3 线程。

Well, some time after making this post I figured it out.好吧,发表这篇文章一段时间后,我想通了。

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

ApiName is the name of the scope which the client uses, so it this case the value should be api . ApiName是客户端使用的范围的名称,因此在这种情况下,该值应为api

ApiSecret is the PRE-HASHED value of the scope secret . ApiSecret范围 secretPRE-HASHED值。

eg if secret value is "test" and it's SHA256 value is 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08, then ApiSecret value should be test例如,如果秘密值是“测试”并且它的 SHA256 值是 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08,那么ApiSecret值应该是测试

So, after figuring this out, the above options config should look like this:因此,在弄清楚这一点之后,上面的选项配置应该是这样的:

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

Note: SHA512 works as well.注意: SHA512 也适用。

To me this seems like a major naming issue.对我来说,这似乎是一个主要的命名问题。

I solved this after analysing this VS solution:我在分析了这个 VS 解决方案后解决了这个问题:

https://github.com/IdentityServer/CrossVersionIntegrationTests https://github.com/IdentityServer/CrossVersionIntegrationTests

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有IdentityServer3 AccessTokenValidation的Identityserver4 - Identityserver4 with IdentityServer3 AccessTokenValidation System.Net.Http DLL 未复制到 bin 文件夹中 - IdentityServer4.AccessTokenValidation - System.Net.Http DLL not copied into bin folder - IdentityServer4.AccessTokenValidation 与Web API示例IdentityServer3的统一 - Unity with web api sample IdentityServer3 使用.net 4.5与Identityserver3验证swagger - Authenticating swagger with identityserver3 using .net 4.5 401 未授权使用 IdentityServer3.AccessTokenValidation - 401 Unauthorized using IdentityServer3.AccessTokenValidation 实现IdentityServer3时不调用Web API 2 DelegateHandler - Web API 2 DelegateHandler Not Invoked When IdentityServer3 Implemented IdentityServer启用参考令牌 - IdentityServer enable reference tokens IdentityServer3 .Net Web API。 收到错误-该请求的授权被拒绝 - IdentityServer3 .Net Web API. Getting error - Authorization has been denied for this request 将 ASP.NET 角色授权与 IdentityServer3 隐式流结合使用 - Using ASP.NET Role Authorisation with IdentityServer3 implicit flow 在 Dot Net Framework 4.8 中使用 IdentityModel 1.9.2 和 IdentityServer3.AccessTokenValidation 的扩展授权进行委派 - Delegation using an extension grant with IdentityModel 1.9.2 and IdentityServer3.AccessTokenValidation in Dot Net Framework 4.8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM