【问题标题】:Using eShopOnContainers .NET microservices architecture - Use AD B2C instead of IdentityServer4 for microservice authentication使用 eShopOnContainers .NET 微服务架构 - 使用 AD B2C 而不是 IdentityServer4 进行微服务身份验证
【发布时间】:2022-08-18 03:40:33
【问题描述】:

我已经下载了 eShopOnContainers 这是一个.NET 微服务示例参考应用程序用于微服务架构和 Docker 容器。

https://github.com/dotnet-architecture/eShopOnContainers

https://docs.microsoft.com/en-us/dotnet/architecture/cloud-native/introduce-eshoponcontainers-reference-app

我认为这非常好,但我想停用使用IdentityServer4Identity.API,并且以后可能会使用Duende IdentityServer。目前我们使用 Azure AD B2C,我想继续使用它。目前这意味着不需要本地令牌生成。

查看Ordering.API - Startup.cs 它使用以下authentication

public static IServiceCollection AddCustomAuthentication(this IServiceCollection services, IConfiguration configuration)
{
    // prevent from mapping \"sub\" claim to nameidentifier.
    JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Remove(\"sub\");

    var identityUrl = configuration.GetValue<string>(\"IdentityUrl\");

    services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationScheme;

    }).AddJwtBearer(options =>
    {
        options.Authority = identityUrl;
        options.RequireHttpsMetadata = false;
        options.Audience = \"orders\";
    });

    return services;
}

在 AD B2C 中,我有一个具有两个不同范围的应用程序,并且我已经能够获得具有这两个范围的访问令牌。但是,将上面的代码与访问令牌一起使用时,当用于新的ASP.NET Core Web API 时,我只会得到 HTTP 401 Unauthorized。我还尝试仅设置options.MetadataAddressoptions.Audience,但使用下面的指南没有运气,HTTP 401 Unauthorized 的结果相同。

https://docs.microsoft.com/en-us/aspnet/core/security/authorization/limitingidentitybyscheme?view=aspnetcore-6.0#use-multiple-authentication-schemes

https://dzimchuk.net/setting-up-your-asp-net-core-2-0-apps-and-services-for-azure-ad-b2c/

代币:

我可以使用默认的 Microsoft 身份平台身份验证使其工作,但我不想将客户端密码添加到每个微服务中。

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApi(builder.Configuration.GetSection(\"AzureAd\"));

我知道 Ocelot 之前被用作 API 网关,但由于对 WebSocket 协议的内置支持而被更改为 Envoy,这是在 eShopOnContainers 中实现的新 gRPC 服务间通信所必需的。一旦我得到令牌工作,我需要在那里改变什么吗?

https://docs.microsoft.com/en-us/dotnet/architecture/microservices/multi-container-microservice-net-applications/implement-api-gateways-with-ocelot

    标签: c# .net asp.net-core microservices identityserver4


    【解决方案1】:

    最后证明这是一个非常简单的错误。创建ASP.NET Core Web API 项目我选择Authentication type: None。在 Progam.cs 中只添加了 app.UseAuthorization(); 而不是 app.UseAuthentication();。注意app.UseAuthentication(); 必须在app.UseAuthorization(); 之前调用。

    当我添加它时,我可以使用以下设置:

    services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationScheme;
    
    }).AddJwtBearer(options =>
    {
        options.MetadataAddress = $"<iss URL>.well-known/openid-configuration?p=<tfp>";
        options.Audience = "<aud GUID>";
    });
    

    无需更改我能看到的 Envoy ApiGateway 中的任何内容。

    如果您收到401 Unauthorized,请查看WWW-Authenticate 响应标头以进行进一步的故障排除。

    【讨论】:

      猜你喜欢
      • 2015-09-11
      • 2021-04-26
      • 1970-01-01
      • 2018-04-12
      • 2020-06-12
      • 1970-01-01
      • 1970-01-01
      • 2020-04-03
      • 2020-05-02
      相关资源
      最近更新 更多