在 Blazor 页面中找不到在 Startup 中添加的声明
Claims added in Startup not found in Blazor page
为了在我们基于 Azure B2C 的 Blazor 应用程序中获得组成员资格功能,我在 Startup.cs 的服务器项目中添加了一些声明。当我检查剃刀页面中的用户对象时,我添加的声明不存在。
Startup.cs, public void Configure():
app.Use(async (context, next) =>
{
if (context.User != null && context.User.Identity.IsAuthenticated)
{
var groups = GetGroupsFromAzureB2c(context.User);
// Attempt A, separate Identity
ClaimsIdentity i = new(context.User.Identity);
i.AddClaims(groups.Select(g => new Claim("Group", g)));
context.User.AddIdentity(i);
// Attempt B: Adding claims to the existing Identity
((ClaimsIdentity)context.User.Identity).AddClaims(groups.Select(g => new Claim("Group", g));
}
await next();
});
page.razor,受保护的覆盖异步任务 OnInitializedAsync():
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
if (user.Identity.IsAuthenticated)
{
var _claims = user.Claims;
}
_claims 仅包含自动添加的 12 个声明,而不是我在 Startup.cs.
中添加的 4 个声明
用户对象有不同的实例吗?我是否错误地添加了声明?
显然,在 Blazor 应用程序中有两个独立的身份在起作用。一个在客户端代码中,另一个在服务器代码中。
我们通过添加继承自 AccountClaimsPrincipalFactory<RemoteUserAccount>
的 CustomUserFactory
解决了这个问题。我们使用 Graph API 从中央方法检索 AzureB2C 角色,我们在 Program.cs 和
中调用客户端
builder.Services.AddMsalAuthentication(options =>
{
builder.Configuration.Bind("AzureAdB2C", options.ProviderOptions.Authentication);
options.ProviderOptions.DefaultAccessTokenScopes.Add("xxx");
})
// here we hook up our custom factory
.AddAccountClaimsPrincipalFactory<CustomUserFactory>();
对于服务器端代码,我们使用相同的中央方法从 AzureB2C 获取角色,我们从 Startup.cs 调用
services.Configure<JwtBearerOptions>(JwtBearerDefaults.AuthenticationScheme, options => {
options.Events.OnTokenValidated = async context => {/*add roles here*/}
为了在我们基于 Azure B2C 的 Blazor 应用程序中获得组成员资格功能,我在 Startup.cs 的服务器项目中添加了一些声明。当我检查剃刀页面中的用户对象时,我添加的声明不存在。
Startup.cs, public void Configure():
app.Use(async (context, next) =>
{
if (context.User != null && context.User.Identity.IsAuthenticated)
{
var groups = GetGroupsFromAzureB2c(context.User);
// Attempt A, separate Identity
ClaimsIdentity i = new(context.User.Identity);
i.AddClaims(groups.Select(g => new Claim("Group", g)));
context.User.AddIdentity(i);
// Attempt B: Adding claims to the existing Identity
((ClaimsIdentity)context.User.Identity).AddClaims(groups.Select(g => new Claim("Group", g));
}
await next();
});
page.razor,受保护的覆盖异步任务 OnInitializedAsync():
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
if (user.Identity.IsAuthenticated)
{
var _claims = user.Claims;
}
_claims 仅包含自动添加的 12 个声明,而不是我在 Startup.cs.
中添加的 4 个声明用户对象有不同的实例吗?我是否错误地添加了声明?
显然,在 Blazor 应用程序中有两个独立的身份在起作用。一个在客户端代码中,另一个在服务器代码中。
我们通过添加继承自 AccountClaimsPrincipalFactory<RemoteUserAccount>
的 CustomUserFactory
解决了这个问题。我们使用 Graph API 从中央方法检索 AzureB2C 角色,我们在 Program.cs 和
builder.Services.AddMsalAuthentication(options =>
{
builder.Configuration.Bind("AzureAdB2C", options.ProviderOptions.Authentication);
options.ProviderOptions.DefaultAccessTokenScopes.Add("xxx");
})
// here we hook up our custom factory
.AddAccountClaimsPrincipalFactory<CustomUserFactory>();
对于服务器端代码,我们使用相同的中央方法从 AzureB2C 获取角色,我们从 Startup.cs 调用
services.Configure<JwtBearerOptions>(JwtBearerDefaults.AuthenticationScheme, options => {
options.Events.OnTokenValidated = async context => {/*add roles here*/}