How can I customize simple membership provider to work with my own database ASP.NET mvc 4

爱⌒轻易说出口 提交于 2019-11-28 04:01:28
manoj sharma

with SimpleMembership there are 2 ways for storing and using that information for authentication.

  1. you may use default (UserProfiles) table, that is in database pointed to by "DefaultConnection" string.

  2. you may use YOUR database and a table therein to be used as replacement of default UserProfiles table.

option 1 is explained very well elsewhere. for option 2 follow steps given below: suppose your database context is mDbContext and table that you want to use for replacement of UserProfiles is Employees.

  1. your Employee model looks like this

    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. your DbContext looks like this

    namespace m.Models
    {
        public class mDBContext : DbContext
        {
             DbSet<Employee> Employees { get; set; }
    ......
    
  3. you need to tell WebSecurity to use your database.

    WebSecurity.InitializeDatabaseConnection("mDBContext", 
      "Employees", "ID", "UserName", autoCreateTables: true);
    
  4. add additional fields in RegisterModel class in AccountModels

    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. In AccountController Register method for HttpPost replace

    WebSecurity.CreateUserAndAccount(model.UserName, model.
    

    with

    WebSecurity.CreateUserAndAccount(model.UserName, model.Password, 
      new {  FirstName = model.FirstName, LastName = model.LastName, 
             Mobile = model.Mobile});
    
  6. rebuild and update database if pending any changes (or add migrations).

Komengem

Follow the answer on similar question to the link below. If you have any more questions let me know.

Similar Question with answer

Update

After reading your first comment, sounds like you need to first understand what MVC is all about before you touch on SimpleMembership. try the following links.

wikipedia

w3schools

MSDN

http://www.asp.net/mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!