如何使用 IEnumerable/Collection 属性 扩展用户身份
How to extend User Identity with an IEnumerable/Collection property
类似于这个问题:
当用户登录时,我想加载我的用户关联的部门。我猜我会像这样向 ApplicationUser
class 添加 属性:
public class ApplicationUser : IdentityUser<Guid, GuidUserLogin, GuidUserRole, GuidUserClaim>
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
public IEnumerable<Department> Departments { get; set; }
}
我的问题是 how/where 我会填充集合,然后我将如何在我的控制器中访问 属性。据我了解,声明对于简单类型是可以的——例如 Id——但它们可以用于集合吗?
我假设一旦我加载了这个 属性,我就可以在每次需要有关用户的信息时查询集合而无需访问数据库 - 这将是经常发生的。
感谢任何帮助。
首先创建集合实体,即Department。然后在其中引用 ApplicationUser 实体的 ID。
假设你使用entity frameworkcode-first,这里是一个例子:
public class ApplicationUser : IdentityUser<Guid, GuidUserLogin, GuidUserRole, GuidUserClaim>
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
public ApplicationUser()
{
Departments = new Collection<Department>();
}
public ICollection<Department> Departments { get; set; }
}
public class Department
{
public string UserId { get; set; }
public int DepartmentId { get; set; }
public ApplicationUser User { get; set; }
protected Department()
{
}
}
类似于这个问题:
当用户登录时,我想加载我的用户关联的部门。我猜我会像这样向 ApplicationUser
class 添加 属性:
public class ApplicationUser : IdentityUser<Guid, GuidUserLogin, GuidUserRole, GuidUserClaim>
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
public IEnumerable<Department> Departments { get; set; }
}
我的问题是 how/where 我会填充集合,然后我将如何在我的控制器中访问 属性。据我了解,声明对于简单类型是可以的——例如 Id——但它们可以用于集合吗?
我假设一旦我加载了这个 属性,我就可以在每次需要有关用户的信息时查询集合而无需访问数据库 - 这将是经常发生的。
感谢任何帮助。
首先创建集合实体,即Department。然后在其中引用 ApplicationUser 实体的 ID。
假设你使用entity frameworkcode-first,这里是一个例子:
public class ApplicationUser : IdentityUser<Guid, GuidUserLogin, GuidUserRole, GuidUserClaim>
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
public ApplicationUser()
{
Departments = new Collection<Department>();
}
public ICollection<Department> Departments { get; set; }
}
public class Department
{
public string UserId { get; set; }
public int DepartmentId { get; set; }
public ApplicationUser User { get; set; }
protected Department()
{
}
}