Entity framework 从ASP.Net Core中Identity User中的个人数据获取数据
我有一个从Identity User继承的类,如下所示Entity framework 从ASP.Net Core中Identity User中的个人数据获取数据,entity-framework,asp.net-core,Entity Framework,Asp.net Core,我有一个从Identity User继承的类,如下所示 public class ApplicationUser:IdentityUser { [PersonalData] public bool IsManager { get; set; } [PersonalData] public string FullName { get; set; } [PersonalData] public s
public class ApplicationUser:IdentityUser
{
[PersonalData]
public bool IsManager { get; set; }
[PersonalData]
public string FullName { get; set; }
[PersonalData]
public string UserNameBW { get; set; }
[PersonalData]
public int DepartmentId { get; set; }
}
public class CustomClaimsPrincipalFactory : UserClaimsPrincipalFactory<ApplicationUser>
{
public CustomClaimsPrincipalFactory(
UserManager<ApplicationUser> userManager,
IOptions<IdentityOptions> optionsAccessor)
: base(userManager, optionsAccessor)
{
}
protected override async Task<ClaimsIdentity>GenerateClaimsAsync(ApplicationUser user)
{
var identity = await base.GenerateClaimsAsync(user);
identity.AddClaim(new Claim("FullName", user.FullName));
return identity;
}
}
User.FindFirst("FullName").Value
现在我想访问当前用户的FullName
,我该怎么做呢
我只通过User.Identity.name
获取名称
提前感谢您可以创建自定义索赔原则工厂,添加自定义索赔并注册 像这样的
public class ApplicationUser:IdentityUser
{
[PersonalData]
public bool IsManager { get; set; }
[PersonalData]
public string FullName { get; set; }
[PersonalData]
public string UserNameBW { get; set; }
[PersonalData]
public int DepartmentId { get; set; }
}
public class CustomClaimsPrincipalFactory : UserClaimsPrincipalFactory<ApplicationUser>
{
public CustomClaimsPrincipalFactory(
UserManager<ApplicationUser> userManager,
IOptions<IdentityOptions> optionsAccessor)
: base(userManager, optionsAccessor)
{
}
protected override async Task<ClaimsIdentity>GenerateClaimsAsync(ApplicationUser user)
{
var identity = await base.GenerateClaimsAsync(user);
identity.AddClaim(new Claim("FullName", user.FullName));
return identity;
}
}
User.FindFirst("FullName").Value
您可以使用UserManager中的FindByName。