Blazor WASM - AzureAD Auth - HttpContext.User.Claims 是空的?
Blazor WASM - AzureAD Auth - HttpContext.User.Claims are Empty?
我正尝试在我正在构建的 Blazor WASM 应用程序上为我的身份平台切换到 Azure AD。我非常密切地关注 Microsoft 的 this documentation。
当我登录应用程序时,客户端应用程序能够显示登录用户名,该名称来自 AuthenticationState 对象。
但是,在服务器端,当我发送一个 HTTP 请求(例如发表评论)时,HttpContext.User.Claims 是空的,而我之前使用的以下行来获取用户 ID returns空:
comment.UserId = HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
我恳求我对 Claims/Identitys/Microsoft Graph 等一无所知,但我仍然不明白为什么用户/声明会是未知的,因为自客户端应用程序以来至少在某一时刻可以访问此信息能够显示用户名...
我还查看了以下 Whosebug/GitHub 个帖子,但没有找到任何解决此问题的方法:
- Blazor with AzureAD Auth, Context.Identity.User.Name is null
- Out of box AAD B2C not grabbing user.identity.name
- User.Identity.Name empty when implementing Azure B2C in Blazor WebAssembly
为了能够在传入的 HTTP 请求中识别用户,我在这里缺少什么?
FWIW,这是我的 Startup.cs class(发出了一些不相关的代码):
public void ConfigureServices(IServiceCollection services)
{
#region Azure Active Directory
services.AddAuthentication(AzureADDefaults.BearerAuthenticationScheme)
.AddAzureADBearer(options => {
Configuration.Bind("AzureAd", options);
}
);
#endregion
services.Configure<JwtBearerOptions>(
AzureADDefaults.JwtBearerAuthenticationScheme, options =>
{
options.TokenValidationParameters.NameClaimType = "name";
//options.TokenValidationParameters.NameClaimType = "preferred_username";
});
services.AddHttpContextAccessor();
services.AddControllersWithViews().AddNewtonsoftJson(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
services.AddRazorPages();
}
和配置():
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseHttpsRedirection();
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
endpoints.MapFallbackToFile("index.html");
});
}
问题是 removing/not 将“api://”方案添加到客户端 ID 导致由于某种原因无法填充声明。很奇怪。 .
我正尝试在我正在构建的 Blazor WASM 应用程序上为我的身份平台切换到 Azure AD。我非常密切地关注 Microsoft 的 this documentation。
当我登录应用程序时,客户端应用程序能够显示登录用户名,该名称来自 AuthenticationState 对象。
但是,在服务器端,当我发送一个 HTTP 请求(例如发表评论)时,HttpContext.User.Claims 是空的,而我之前使用的以下行来获取用户 ID returns空:
comment.UserId = HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
我恳求我对 Claims/Identitys/Microsoft Graph 等一无所知,但我仍然不明白为什么用户/声明会是未知的,因为自客户端应用程序以来至少在某一时刻可以访问此信息能够显示用户名...
我还查看了以下 Whosebug/GitHub 个帖子,但没有找到任何解决此问题的方法:
- Blazor with AzureAD Auth, Context.Identity.User.Name is null
- Out of box AAD B2C not grabbing user.identity.name
- User.Identity.Name empty when implementing Azure B2C in Blazor WebAssembly
为了能够在传入的 HTTP 请求中识别用户,我在这里缺少什么?
FWIW,这是我的 Startup.cs class(发出了一些不相关的代码):
public void ConfigureServices(IServiceCollection services)
{
#region Azure Active Directory
services.AddAuthentication(AzureADDefaults.BearerAuthenticationScheme)
.AddAzureADBearer(options => {
Configuration.Bind("AzureAd", options);
}
);
#endregion
services.Configure<JwtBearerOptions>(
AzureADDefaults.JwtBearerAuthenticationScheme, options =>
{
options.TokenValidationParameters.NameClaimType = "name";
//options.TokenValidationParameters.NameClaimType = "preferred_username";
});
services.AddHttpContextAccessor();
services.AddControllersWithViews().AddNewtonsoftJson(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
services.AddRazorPages();
}
和配置():
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseHttpsRedirection();
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
endpoints.MapFallbackToFile("index.html");
});
}
问题是 removing/not 将“api://”方案添加到客户端 ID 导致由于某种原因无法填充声明。很奇怪。