How do I create a custom membership for ASP.NET MVC 2 based on the ASP.NET membership provider?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I have created a new project containing a custom membership provider and overrode the
ValidateUsermethod from theMembershipProviderabstract class:Then I connected that provider to my ASP.NET MVC 2 project by adding a reference and pointing it out from my web.config:
I do need to create a custom class that inherits the
RoleProviderabstract class and overrides theGetRolesForUsermethod.The ASP.NET MVC Authorizing uses that method to find out which roles are assigned to the current logged-on user and makes sure the user is permitted to access the controller action.
Here are the steps we need to take:
1) Create a custom class that inherits the RoleProvider abstract class and overrides the GetRolesForUser method:
2) Connect the role provider with the ASP.NET MVC 2 application via our web.config:
3) Set the Authorize(Roles=”xxx,yyy”) above the wanted Controller / Action:
That’s it! Now it works!
4) Optional: set a custom
Authorizeattribute so we can redirect an unwanted role to an AccessDenied Page:Now we can use our own made attribute to redirect our users to access denied view:
That’s it!
Super duper!
Here are some of the links I’ve used to get all this info:
Custom role provider:
http://davidhayden.com/blog/dave/archive/2007/10/17/CreateCustomRoleProviderASPNETRolePermissionsSecurity.aspx
I hope this info helps!