SimpleMembership with custom database schema in ASP.NET MVC 4

前端 未结 7 961
闹比i
闹比i 2020-12-04 21:50

I want to enable the ASP.NET MVC 4\'s SimpleMembership API to integrate with my own database schema. I have a plain and simple table in my database called Users

7条回答
  •  醉梦人生
    2020-12-04 22:22

    You could use both your custom Users table along with the SimpleMembership tables. This method has worked great for me in the past.

    For example, in your Register method in the AccountController, you would register a new user by adding the user to your own Users table. Then add this user to the SimpleMembership UserProfile table using the Id of the user you added to the Users table.

    using (var context = new MyDatabaseEntities())
    {
        User newUser = new User(){
          Name = model.Name,
          Email = model.Email,
          IsDeleted = false
        }
    
        // Add the user to your custom Users table
        context.Users.Add(newUser);
        context.SaveChanges();
    
        // Add this user to the SimpleMembership table using the same Id as the custom Users table
        WebSecurity.CreateUserAndAccount(newUser.Id.ToString(), model.Password);
    
        // Log the user in
        WebSecurity.Login(newUser.Id.ToString(), model.Password);
    }
    

提交回复
热议问题