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

前端 未结 2 1418
野趣味
野趣味 2020-12-07 19:34

I’m exploring ASP.NET MVC 4 these days. I will be pleased if someone can help by answering my question .

I am building an academic project \"Project management and s

相关标签:
2条回答
  • 2020-12-07 20:09

    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

    0 讨论(0)
  • 2020-12-07 20:14

    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).

    0 讨论(0)
提交回复
热议问题