ASP MVC 3 Basic Register / Login / Logout on a dif

2019-01-29 10:47发布

问题:

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 already have designed my own 'Users' table, which I'd like a simple Registration / Log in / Log out to be implemented.

Are there are good tutorials showing me how to do this or any advice from yourselves? Many thanks for your time.

回答1:

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.



回答2:

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/