ASP MVC 3 Basic Register / Login / Logout on a different table

后端 未结 2 1207
梦毁少年i
梦毁少年i 2020-12-22 09:53

I have seen the default \'Account\' Model and Controller which comes default with a standard MVC3 Application, how ever, as I have generated my Database first.

I al

2条回答
  •  长情又很酷
    2020-12-22 10:36

    It's simple. Create your class derived from the abstract class MembershipProvider

    public class MyMembershipProvider : MembershipProvider
    {
    
    }
    

    More at: http://msdn.microsoft.com/en-us/library/system.web.security.membershipprovider.aspx

    Do the same for RoleProvider if you need it.

    public class MyRoleProvider : RoleProvider
    {
    
    }
    

    More at: http://msdn.microsoft.com/en-us/library/system.web.security.roleprovider.aspx

    Implement only the methods you will use and that's all. Start with ValidateUser() ( http://msdn.microsoft.com/en-us/library/system.web.security.membershipprovider.validateuser.aspx)

    Don't forget to point your provider, that is in this case MyMembershipProvider to web.config in section.

    Don't complicated it as in almost every tutorial/blog post out there is doing, it's a simple task.

    UPDATE:

    In the RoleProvider you only need to implement

    public override string[] GetAllRoles()
            {
                return RoleRepository.GetAllRoles();
            }
    
            public override string[] GetRolesForUser(string username)
            {
                return RoleRepository.GetRolesForUser(username);
            }
    
    public override bool IsUserInRole(string username, string roleName)
            {  
                return RoleRepository.IsUserInRole(username, roleName);
            }
    

    In the MembershipProvider you only need to implement

    public override bool ValidateUser(string username, string password)
            {
                return MembershipRepository.IsUserValid(username,password);
            }
    

    You could always use your own ValidateUser() method regardless of the method in the MembershipProvider.

提交回复
热议问题