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

后端 未结 2 1201
梦毁少年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:27

    You can implement your custom Membership provider:

    http://www.asp.net/web-forms/videos/how-do-i/how-do-i-create-a-custom-membership-provider

    http://theintegrity.co.uk/2010/11/asp-net-mvc-2-custom-membership-provider-tutorial-part-1/

    0 讨论(0)
  • 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 <system.web> <membership> <providers> 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.

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