【发布时间】:2014-10-26 22:35:49
【问题描述】:
我正在创建 MVC5 应用程序,并且我已经在使用 ASP.NET Identity 来创建用户。所以,我已经有了 AspNetUsers 表,每当用户注册时,我都会在那里获得一个条目。我还有一个管理员角色,我在其中手动指定哪个注册用户是管理员。另一方面,我还需要注册企业,就像普通用户一样,他们将能够登录、注册和做一些事情。关键是他们将拥有一些与普通用户相似和不同的字段。例如,他们还会有电子邮件地址、密码(我想像普通用户一样对其进行哈希处理)、电子邮件确认、唯一 ID 等。但是他们有不同的字段来获取更多信息,比如他们的地址、zip ,国家,类别等普通用户没有的。如何在 MVC 中实现这一点?
我应该做一些类似于 ApplicationUser 类的事情吗?
public class ApplicationUser : IdentityUser
我的意思是,我应该从 IdendityUser 继承我的业务模型吗?如果是,我的模型如何知道要使用 IdentityUser 中的哪些字段,哪些不使用?
这是我目前的商业模式:
public class Business
{
public int BusinessID { get; set; }
public string BusinessName { get; set; }
[ForeignKey("Category")]
public int CategoryID { get; set; }
public virtual Category Category { get; set; }
[ForeignKey("Subcategory")]
public int SubcategoryID { get; set; }
public virtual Subcategory Subcategory { get; set; }
public string BusinessAddress { get; set; }
public string BusinessZip { get; set; }
public string BusinessPhone { get; set; }
public string BusinessDescription { get; set; }
public string Facebook { get; set; }
public string Twitter { get; set; }
public byte[] ImageData { get; set; }
public string ImageMimeType { get; set; }
[Range(0.0, 5.0)]
public double BusinessRating { get; set; }
public virtual ICollection<Review> Reviews { get; set; }
}
因此,除了这些字段之外,我希望我的表包含类似于 AspNetUsers 的内容,例如 Email、EmailConfirmed、PasswordHash、SecurityStamp 等。
编辑:
请注意,我在商业模式中的某些字段是必需的。您还可以在下面找到我的 ApplicationUser 类。
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
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;
}
}
【问题讨论】:
标签: asp.net asp.net-mvc entity-framework