Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable Role Manager with SimpleMembership on ASP.NET MVC 4

I'm wondering how to use the Role Manager feature with the SimpleMembership system included in ASP.NET MVC 4, specifically how to build a controller that manages all data associated with Roles, and using the webpage_Roles table that SimpleMembership creates when a project is created with a Internet Template. Is there a way to automate this within the Login/Register actions in the AccountController?

like image 253
lio Avatar asked Sep 05 '12 15:09

lio


2 Answers

Well, I am no expert on ASP.NET MVC4 but as a task I have set for myself, I wanted to create Role Based site access.

ASP.NET MVC4 is an excellent resource! I do have complaints about the lack of information and difficulty to implement Roles in MVC4.

To achieve the task one can implement SimpleMembership and SimpleRoles. See the below links:

  1. Using SimpleMembership With ASP.NET WebPages by Matthew M. Osborn
  2. SimpleMembership, Membership Providers, Universal Providers and the new ASP.NET 4.5 Web Forms and ASP.NET MVC 4 templates by Jon Galloway

The above two links explain a lot and have some very basic code examples but unfortunately the solution download is not available in the first URL.

In addition to the above:

  1. Customize the SimpleMembership in ASP.NET MVC 4.0 by thangchung
  2. ASP.NET MVC 4 Sample on MSDN

This last article goes into a much better detail and also gives source code to peruse.

like image 139
Rusty Nail Avatar answered Sep 27 '22 16:09

Rusty Nail


In web.config add the following

<profile defaultProvider="SimpleProfileProvider">
      <providers>
        <add name="SimpleProfileProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" connectionStringName="DNMXEntities" applicationName="/" />
      </providers>
    </profile>
    <membership defaultProvider="SimpleMembershipProvider">
      <providers>
        <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
      </providers>
    </membership>
    <roleManager defaultProvider="SimpleRoleProvider">
      <providers>
        <add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData" />
      </providers>
    </roleManager>
like image 28
Yogui Sadhaka Avatar answered Sep 27 '22 16:09

Yogui Sadhaka