几天来,我试图为Blazor应用程序声明声明。我终于找到了一种应该有效的方法,但由于某种原因,我在索赔列表中找不到我的索赔。我创建了以下AppClaimsPrincipalFactory:
public class AppClaimsPrincipalFactory : UserClaimsPrincipalFactory<ApplicationUser>
{
public AppClaimsPrincipalFactory(UserManager<ApplicationUser> userManager, IOptions<IdentityOptions> optionsAccessor) : base(userManager, optionsAccessor)
{
}
public override async Task<ClaimsPrincipal> CreateAsync(ApplicationUser user)
{
var id = await GenerateClaimsAsync(user);
if (user != null)
{
id.AddClaim(new Claim(ClaimTypes.GivenName, user.FirstName));
}
return new ClaimsPrincipal(id);
var principal = await base.CreateAsync(user);
if (!string.IsNullOrWhiteSpace(user.FirstName))
{
((ClaimsIdentity)principal.Identity).AddClaim(new Claim(ClaimTypes.GivenName, user.FirstName));
}
if (!string.IsNullOrWhiteSpace(user.LastName))
{
((ClaimsIdentity)principal.Identity).AddClaim(new Claim(ClaimTypes.Surname, user.LastName));
}
return principal;
}
}
我在Startup.cs文件的ConfigureServices中添加了以下代码:
services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, AppClaimsPrincipalFactory>();
有人能帮我找出我的代码出了什么问题吗?提前谢谢。
更新
感谢Brian Parker为我铺平了道路。为了在其他人需要的情况下使其发挥作用,我将AppClaimsPrincipalFactory类更改为:
public class AppClaimsPrincipalFactory : UserClaimsPrincipalFactory<ApplicationUser>
{
public AppClaimsPrincipalFactory(UserManager<ApplicationUser> userManager, IOptions<IdentityOptions> optionsAccessor) : base(userManager, optionsAccessor)
{
}
protected override async Task<ClaimsIdentity> GenerateClaimsAsync(ApplicationUser user)
{
ClaimsIdentity claims = await base.GenerateClaimsAsync(user);
claims.AddClaim(new Claim("name", "Aris"));
claims.AddClaim(new Claim("customClaim", "test value"));
claims.AddClaim(new Claim(ClaimTypes.GivenName, user.FirstName));
claims.AddClaim(new Claim(ClaimTypes.Surname, user.LastName));
return claims;
}
}
我还更改了代码服务。在我的Startup.cs文件中添加DefaultIdentity:
services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddClaimsPrincipalFactory<AppClaimsPrincipalFactory>();
其结果具有以下权利要求值:
name: ["myemail@hotmail.com","Aris"]
customClaim: test value
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname: Aristotelis
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname: Pitaridis
谢谢你,布莱恩·帕克。你成就了我的一天。
尝试:
services.AddScoped<AppClaimsPrincipalFactory>();
services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddClaimsPrincipalFactory<AppClaimsPrincipalFactory>();
services.AddIdentityServer()
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>(options =>
{
options.IdentityResources["openid"].UserClaims.Add(ClaimTypes.GivenName);
options.ApiResources.Single().UserClaims.Add(ClaimTypes.GivenName);
options.IdentityResources["openid"].UserClaims.Add(ClaimTypes.Surname);
options.ApiResources.Single().UserClaims.Add(ClaimTypes.Surname);
});