C# 如何在dotnet core中访问Identity中的扩展属性
感觉有点卡住了:我能够向ApplicationUser添加属性,因此:C# 如何在dotnet core中访问Identity中的扩展属性,c#,.net-core,C#,.net Core,感觉有点卡住了:我能够向ApplicationUser添加属性,因此: public class ApplicationUser : IdentityUser { [DataType(DataType.Text)] public string CustomUserName { get; set; } [DataType(DataType.Text)] public string Address { get;
public class ApplicationUser : IdentityUser
{
[DataType(DataType.Text)]
public string CustomUserName { get; set; }
[DataType(DataType.Text)]
public string Address { get; set; }
[DataType(DataType.Text)]
public string City { get; set; }
[DataType(DataType.PostalCode)]
public string ZipCode { get; set; }
}
它们显示在我的Register.cshtml视图中,并通过Register方法添加到数据库中。到目前为止,我使用的是dotnetcore提供的默认设置,带有Identity
问题是一旦他们在那里,如何访问他们。UserManager有一些默认方法,比如GetEmailAsync
,但是获取其他新添加属性的过程是什么
我曾考虑过使用扩展方法,但当我深入研究原始源代码(试图模仿GetEmailAsync
的功能)时,我承认我有点迷失了,我不确定在任何情况下是否应该这样做
我也环顾了一下四周,这部分似乎什么也做不到。有一些例子说明了如何将属性添加到注册中,但是没有人说一旦属性在注册中如何检索它们
那么,在我新打造的房产中,最好的方式是什么呢 用户管理器类是一个通用类。如您所见,您将需要一个UserManager
的实例
UserManager
类公开方法Task FindByIdAsync(string userId)
,在您的情况下,该方法将返回ApplicationUser
的实例
因此,您所需要做的就是调用
ApplicationUser myUser=myUserManager.FindByIdAsync(“myUserId”)代码>请参见下面的答案。