【发布时间】:2017-02-07 10:33:04
【问题描述】:
类似这个问题:How to extend available properties of User.Identity
当用户登录时,我想加载与我的用户关联的部门。我猜我会像这样向ApplicationUser 类添加一个属性:
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; }
}
我的问题是我将如何/在哪里填充集合,然后如何在我的控制器中访问该属性。据我了解,声明对于简单类型(例如 Id)来说是可以的,但它们可以与集合一起使用吗?
我假设一旦我加载了这个属性,我就可以在每次需要有关用户的信息时查询集合而无需访问数据库 - 这很常见。
任何帮助表示赞赏。
【问题讨论】:
-
也许有一个域实体用于存储集合并引用ApplicationUser的Id来建立关系?
-
在ApplicationUser类的构造函数中?
标签: c# asp.net-mvc asp.net-identity claims-based-identity