Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 从ASP.Net Core中Identity User中的个人数据获取数据_Entity Framework_Asp.net Core - Fatal编程技术网

Entity framework 从ASP.Net Core中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

我有一个从Identity User继承的类,如下所示

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。