Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 support system" . I have designed my own database , I have my own tables for the users in my database (two kinds of users : Employee who will execute tasks , and a client who assign/hire for tasks ) ,I was about creating a new membership provider but I realized that "It's a waste of time - reinventing the wheel".

Now , I am building a membership model on ASP.NET MVC4 using SimpleMembership (It's the future of membership services for MVC applications ).It provides a more concise membership provider to the ASP.NET framework, and supports OAuth additionally.

1- I created an out-of-the-box ASP.NET MVC 4 internet application to customize the Login, Signup and User management logic to maintain the user-profile table. I added three roles : Admin , Employee , Client

Going through this blogpost , I am able to customize the registration http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-codefirst-and-custom-user-properties/

2- Now , I am working synchronize this table with the tables of the users that I have in my own database . Taking in consideration that I have added another field of "Account Type" Asking the user during the registration to create a specific profile .

Any help greatly appreciated.

Cheers

like image 542
ImnotaGeek Avatar asked Apr 03 '13 00:04

ImnotaGeek


1 Answers

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

like image 50
manoj sharma Avatar answered Nov 02 '22 05:11

manoj sharma