我有一个Angular应用程序和一个Web Api应用程序(.Net 5.0),我需要使用Azure AD B2C对用户进行身份验证。因此,当用户需要访问某些受保护的路由时,他将被重定向到B2C登录页面,进行身份验证并重定向回Angular应用程序。这个Angular部分是没有问题的。
现在,当Angular应用程序向受保护的Web API(.Net 5)发出请求时,这个Web API应该授权该请求。我以前在早期版本的.Net Core中做过这件事。但是这是新的、闪亮的Microsoft Identity web。它不起作用。
设置如下:
B2C的appsettings.json
使用这个设置,我只得到了一个非常详细的错误提示:
System.InvalidOperationException: IDX20803: 无法从 'System.String' 获取配置。 ---> System.IO.IOException: IDX20807: 无法从 'System.String' 检索文档。HttpResponseMessage: 'System.Net.Http.HttpResponseMessage',HttpResponseMessage.Content: 'System.String'。 at Microsoft.IdentityModel.Protocols.HttpDocumentRetriever.GetDocumentAsync(String address, CancellationToken cancel) at Microsoft.IdentityModel.Protocols.OpenIdConnect.OpenIdConnectConfigurationRetriever.GetAsync(String address, IDocumentRetriever retriever, CancellationToken cancel) at Microsoft.IdentityModel.Protocols.ConfigurationManager
然后我将这个“helper”糖配置更改为以下内容:
现在,当Angular应用程序向受保护的Web API(.Net 5)发出请求时,这个Web API应该授权该请求。我以前在早期版本的.Net Core中做过这件事。但是这是新的、闪亮的Microsoft Identity web。它不起作用。
设置如下:
B2C的appsettings.json
"AzureAdB2C": {
"TenantId": "[Tenant Guid]",
"Instance": "https://[tenant name].b2clogin.com/",
"ClientId": "[web api client Id]",
"Domain": "[tenant name].onmicrosoft.com",
"SignInPolicyId": "B2C_1_signin"
},
startup.cs - ConfigureServices()
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(options =>
{
Configuration.Bind("AzureAdB2C", options);
options.TokenValidationParameters.NameClaimType = "name";
},
options => { Configuration.Bind("AzureAdB2C", options); });
// Creating policies that wraps the authorization requirements
services.AddAuthorization();
services.AddApplication();
services.AddInfrastructure(Configuration);
services.AddRouting(options => options.LowercaseUrls = true);
services.AddMemoryCache();
services.AddControllers().AddNewtonsoftJson();
startup.cs - Configure()
...
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
// endpoints.MapRazorPages();
endpoints.MapControllers();
});
使用这个设置,我只得到了一个非常详细的错误提示:
System.InvalidOperationException: IDX20803: 无法从 'System.String' 获取配置。 ---> System.IO.IOException: IDX20807: 无法从 'System.String' 检索文档。HttpResponseMessage: 'System.Net.Http.HttpResponseMessage',HttpResponseMessage.Content: 'System.String'。 at Microsoft.IdentityModel.Protocols.HttpDocumentRetriever.GetDocumentAsync(String address, CancellationToken cancel) at Microsoft.IdentityModel.Protocols.OpenIdConnect.OpenIdConnectConfigurationRetriever.GetAsync(String address, IDocumentRetriever retriever, CancellationToken cancel) at Microsoft.IdentityModel.Protocols.ConfigurationManager
1.GetConfigurationAsync(CancellationToken cancel) --- 内部异常堆栈跟踪的结尾 --- at Microsoft.IdentityModel.Protocols.ConfigurationManager
1.GetConfigurationAsync(CancellationToken cancel)
at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleAuthenticateAsync()
at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleAuthenticateAsync()
at Microsoft.AspNetCore.Authentication.AuthenticationHandler`1.AuthenticateAsync()
at Microsoft.AspNetCore.Authentication.AuthenticationService.AuthenticateAsync(HttpContext context, String scheme)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)然后我将这个“helper”糖配置更改为以下内容:
services.AddAuthentication(options => { options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; })
.AddJwtBearer(jwtOptions =>
{
jwtOptions.Authority = $"{Configuration["AzureAdB2C:Instance"]}/{Configuration["AzureAdB2C:TenantId"]}/{Configuration["AzureAdB2C:SignInPolicyId"]}/v2.0/";
jwtOptions.Audience = Configuration["AzureAdB2C:ClientId"];
jwtOptions.Events = new JwtBearerEvents
{
OnAuthenticationFailed = AuthenticationFailed
};
});
它有效!
那么,有没有关于如何真正配置MS Identity Web的好文档?为什么异常中没有精确的描述告诉我们缺少什么?
更新
感谢回答,但是这个Identity Web实现真的很差。
这个异常的原因是SignUpSignInPolicyId
未定义或拼写错误!Microsoft团队,你必须将其添加到异常中!
- 添加有意义的异常
- 如果我不想使用SignUpSignIn策略(我不需要任何陌生人来注册我的应用程序),为什么它没有被记录和支持?
- 使用可选日志记录,以告诉开发人员出了什么问题/丢失了什么!
- 使用Builder模式创建配置。然后在特定情况下更容易确定缺少哪个配置部分。
Microsoft.Identity.Web
的痛苦,文档和支持实在是太差了,尤其是针对.Net 5.0。 - undefined