我下载了eShopOnContainers,这是一个.NET微服务示例参考应用程序,用于微服务体系结构和Docker容器。
https://github.com/dotnet-architecture/eShopOnContainers
我认为这真的很好,但是我想退役使用IdentityServer4
的IdentityServer4
,并且以后可能会使用Duende IdentityServer
。目前,我们使用Azure 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中,我有一个具有两个不同作用域的应用程序,并且能够获得这两个作用域的访问令牌。然而,使用上面的代码和访问令牌,我只是得到一个HTTP401未经授权时,对一个新的ASP.NET Core Web API
使用。我还尝试只设置options.MetadataAddress
和options.Audience
,但使用下面的指南却没有成功,与HTTP401未经授权的结果相同。
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协议,gRPC在eShopOnContainers中实现了新的服务间通信,因此被更改为特使。有什么我需要改变的东西,一旦我得到的令牌工作?
发布于 2022-04-14 15:23:20
最后,这是一个非常简单的错误。创建我选择的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>";
});
没有必要改变ApiGateway特使的任何我能看到的东西。
如果收到401 Unauthorized
,请查看WWW-Authenticate
响应头,以进一步排除故障。
https://stackoverflow.com/questions/71874002
复制相似问题