Azure active directory Blazor WASM-AzureAD Auth-HttpContext.User.Claims是否为空?
我正试图在我正在构建的Blazor WASM应用程序上为我的身份平台切换到Azure广告。我和微软的关系非常密切 当我登录到应用程序时,客户端应用程序能够显示登录的用户名,该用户名来自AuthenticationState对象 但是,在服务器端,当我发送HTTP请求(例如发布注释)时,HttpContext.User.Claims是空的,我之前用于获取userId的以下行返回null:Azure active directory Blazor WASM-AzureAD Auth-HttpContext.User.Claims是否为空?,azure-active-directory,asp.net-identity,microsoft-graph-api,blazor,Azure Active Directory,Asp.net Identity,Microsoft Graph Api,Blazor,我正试图在我正在构建的Blazor WASM应用程序上为我的身份平台切换到Azure广告。我和微软的关系非常密切 当我登录到应用程序时,客户端应用程序能够显示登录的用户名,该用户名来自AuthenticationState对象 但是,在服务器端,当我发送HTTP请求(例如发布注释)时,HttpContext.User.Claims是空的,我之前用于获取userId的以下行返回null: comment.UserId = HttpContext.User.FindFirstValue(ClaimT
comment.UserId = HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
我承认对声明/标识/Microsoft Graph等不太了解,但我仍然不知道为什么用户/声明是未知的,因为客户端应用程序能够显示用户的姓名,所以至少有一点可以访问此信息
我还查看了以下StackOverflow/GitHub帖子,但没有找到任何解决此问题的方法:
问题是,由于某些原因,删除/未在客户端ID前添加“api://”方案导致该方案无法填充声明。很奇怪 您是否创建了自定义用户工厂来处理角色和组声明?我没有,但在设置文档中也没有将其作为要求调用。我找到了原因,客户端Id没有在方案/协议前加上前缀,这似乎很奇怪,它有这种效果。这条特定的线在这里被称为:,但他们把它描述为“尝试任何一种”,我觉得这很奇怪。是否有用于处理角色/组声明的自定义用户工厂的教程文档/概述?我真的很难找到这个过程的概述。
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");
});
}