0

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.

McGarnagle
  • 101,349
  • 31
  • 229
  • 260
Sven
  • 11
  • 1
  • 3
  • Duplicate question I think. See here: http://stackoverflow.com/questions/5701673/custom-membershipprovider-in-net-4-0 – McGarnagle Apr 08 '12 at 20:10

2 Answers2

3

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.

Matija Grcic
  • 12,963
  • 6
  • 62
  • 90
  • Many thanks for this structured response! Could you provide an insight as to what I'd need to fill out in order to have a simple register/log in/log out on another table please? – Sven Apr 09 '12 at 00:17
1

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/

Evgeniy Labunskiy
  • 2,012
  • 3
  • 27
  • 45