我正在创建 MVC5 应用程序,并且已经在使用 ASP.NET Identity 来创建用户。因此,我已经有了 AspNetUsers 表,每当用户注册时,我都会在那里获得一个条目。我还有一个管理员角色,我可以手动指定哪个注册用户是管理员。另一方面,我还需要注册企业,就像普通用户一样,他们将能够登录、注册和做一些事情。关键是他们将拥有一些与普通用户相似和不同的字段。例如,他们还将拥有电子邮件地址、密码(我希望像普通用户一样对其进行哈希处理)、电子邮件确认、唯一 ID 等。但是他们有不同的字段来获取更多信息,例如他们的地址、邮政编码、国家、类别等普通用户没有的。我怎样才能在 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 等。
EDIT:
请注意,我的业务模型中的一些字段是必需的。在下面您还可以找到我的 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;
}
}