-->

如何自定义简单成员提供我自己的数据库ASP.NET MVC 4工作(How can I custom

2019-08-19 04:00发布

我探索ASP.NET MVC 4这些天。 我会很高兴,如果有人可以回答我的问题有所帮助。

我建立一个学术工程“项目管理与支持系统”。 我设计我自己的数据库,我有我自己对我的数据库中的用户表(两种用户:员工谁将会执行任务,和客户,谁指派/聘请的任务),我是如何创建一个新的成员提供,但我意识到,“这是在浪费时间 - 重新发明轮子”。

现在,我对ASP.NET MVC4使用SimpleMembership(它的会员服务,为MVC应用的未来)。它提供了一个更简洁的成员提供的ASP.NET框架,并支持OAuth的另外建立一个会员制模式。

1 - 我创建了一个彻头彻尾的现成ASP.NET MVC 4互联网应用自定义登录,注册和用户管理逻辑来维持用户概况表。 我增加了三个角色:管理员,员工,客户端

通过这个博客帖子去,我能够定制注册http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-codefirst-and-定制用户的属性/

2 - 现在,我的工作与我在我自己的数据库用户的表同步此表。 在这方面采取我已经加入的“帐户类型”在注册时要求用户创建一个特定的轮廓的另一场考虑。

任何帮助极大的赞赏。

干杯

Answer 1:

与SimpleMembership有2种方式来存储和使用该信息进行认证。

  1. 你可以使用默认值(的UserProfiles)表,即在数据库中“DefaultConnection”指向的字符串。

  2. 你可以使用你的数据库和表内用作替换默认的UserProfiles表。

选项1是其他地方所解释得非常好。 下面给出的选项2的后续步骤:假设你的数据库环境是mDbContext和表格,你要用于替换的UserProfiles的是员工。

  1. 你的员工的模型看起来像这样

     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; } ......... 
  2. 你的DbContext看起来是这样的

     namespace m.Models { public class mDBContext : DbContext { DbSet<Employee> Employees { get; set; } ...... 
  3. 你需要告诉WebSecurity使用你的数据库。

     WebSecurity.InitializeDatabaseConnection("mDBContext", "Employees", "ID", "UserName", autoCreateTables: true); 
  4. 在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; } } 
  5. 在的AccountController注册方法HttpPost更换

     WebSecurity.CreateUserAndAccount(model.UserName, model. 

     WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { FirstName = model.FirstName, LastName = model.LastName, Mobile = model.Mobile}); 
  6. 重建如果挂起的任何更改更新数据库(或添加迁移)。



Answer 2:

按照类似的问题的答案,下面的链接。 如果您有任何其他问题让我知道。

类似的问题与答案

更新

读你的第一个评论后,听起来像是你有必要先了解MVC是所有关于你触摸SimpleMembership之前。 请尝试以下链接。

维基百科

W3Schools的

MSDN

http://www.asp.net/mvc



文章来源: How can I customize simple membership provider to work with my own database ASP.NET mvc 4