12

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

ImnotaGeek
  • 207
  • 1
  • 4
  • 13

2 Answers2

26

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

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
manoj sharma
  • 276
  • 2
  • 3
0

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

Community
  • 1
  • 1
Komengem
  • 3,662
  • 7
  • 33
  • 57
  • I was checking the same Article an hour before . It doesn't work out . From which Part of the project should I save the profile in my database (I mean which class should I use to inject the profile from the User-Profile to my own database ) ! – ImnotaGeek Apr 03 '13 at 00:16
  • You haven't really asked the question, i gave you an answer based on the title of your post. Either way, this link should work, share your code, maybe that way i could provide you with a specific solution. From the sound of it, you need to create a relationship between UserProfile and your ExtednedUser eg. Employee – Komengem Apr 03 '13 at 00:21
  • Thanks ! I am in this phase now , All what I have built is similar to this code shared here . http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-codefirst-and-custom-user-properties/ – ImnotaGeek Apr 03 '13 at 00:22
  • @ImnotaGeek That link you are following will not help you if you want to take control and include SimpleMembership in your database. take a look at this link again, and ask questions where you getting stuck http://blog.spontaneouspublicity.com/including-asp-net-simple-membership-tables-as-part-of-your-entity-framework-model – Komengem Apr 03 '13 at 00:28
  • Thanks ! I went through the Article ; I have applied successfully all the steps . Should I insert the data After the registration (I am not that good with MVC ), What class should I modify ! – ImnotaGeek Apr 03 '13 at 00:50
  • @ImnotaGeek Read through the links i posted in the updated answer. I can't help much if you don't understand what Model-View-Controller(MVC) architecture does. – Komengem Apr 03 '13 at 01:21
  • Thanks ! I think that I found what I am looking for : http://stackoverflow.com/questions/15177556/customizing-simplemembership-in-mvc-4 – ImnotaGeek Apr 03 '13 at 02:11