使用自定义“用户”表的ASP.NET身份
问题描述:
我想使用OWIN
。但是我需要用户表中更多的列。我也想用NHibernate
。使用自定义“用户”表的ASP.NET身份
这样做的方法是什么? 从OWIN
扩展用户表。我不知道这是否可能。其次,我不知道我能否缓存问题。
或者,我为我自己的用户数据创建第二个用户表,并将“UserID”属性作为引用OWIN
的用户表的外键?
答
反正我会说使用Asp.NET身份。如果你想为你的ApplicationUser
您可以添加它们像下面更多的属性:
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
// Your Extended Properties
public string FirstName{ get; set; }
public string LastName{ get; set; }
//...
}
你甚至可以创建User.Identity
方法一样容易地获得属性的值作为GetUserId()
方法。看看this。
这正是Identity为此设计的:可扩展性。如果你想要'ApplicationUser'上的额外属性,只需添加它们。就像任何其他实体一样。 Owin与身份或认证无关。身份恰好恰好是一个欧文中间件。 –
okey。如果我有表没有引用用户表(和\t 相反),这是从ApplicationUser完全独立的实体,我必须使用NHibernate获取数据? – Xeddon
@Xeddon根据标识和ApplicationUser处理的任何事情都由它自己的DbContext处理,如果您有其他与ApplicationUser无关的表由您自己的ORM处理,但是如果您希望在这些表中拥有UserId,您可以轻松地在NHibernate中将它们作为外键映射到ApplicationUser表。 – Valkyrie