使用SimpleMembership进行身份验证有两种存储和使用信息的方式。
您可以使用默认的(UserProfiles)表,该表位于“DefaultConnection”字符串指向的数据库中。
您可以使用您自己的数据库和其中的一个表来替换默认的UserProfiles表。
选项1在其他地方已经非常好地解释了。 对于选项2,请按照以下步骤进行操作:
假设您的数据库上下文为mDbContext,并且要用作替换UserProfiles的表为Employees。
您的Employee模型如下所示:
namespace m.Models
{
public class Employee
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Mobile { get; set; }
public Designation Designation { get; set; }
.........
你的 DbContext 看起来像这样
namespace m.Models
{
public class mDBContext : DbContext
{
DbSet<Employee> Employees { get; set; }
......
你需要告诉WebSecurity使用你的数据库。
WebSecurity.InitializeDatabaseConnection("mDBContext",
"Employees", "ID", "UserName", autoCreateTables: true);
在AccountModels中的RegisterModel类中添加额外的字段
public class RegisterModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Mobile { get; set; }
public Designation Designation { get; set; }
}
在 AccountController 的 Register 方法中,将 HttpPost 替换为
。
WebSecurity.CreateUserAndAccount(model.UserName, model.
WebSecurity.CreateUserAndAccount(model.UserName, model.Password,
new { FirstName = model.FirstName, LastName = model.LastName,
Mobile = model.Mobile});
如果有任何更改需要处理(或添加迁移),请重建并更新数据库。