Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mvc Runtime error - The Role Manager feature has not been enabled

I am trying to implement basic role management manually. When a user logs in if they are admin or not they are added to a role

I am getting this error:Exception Details: System.Configuration.Provider.ProviderException: The Role Manager feature has not been enabled.

This is the method that causes the error:

 public void addUserToRole(String user, String role)
        {
            if (!Roles.RoleExists(role))
                Roles.CreateRole(role);

            Roles.AddUserToRole(user, role);
        }

here is my web.config

 <profile defaultProvider="DefaultProfileProvider">
      <providers>
        <add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxx" connectionStringName="DefaultConnection" applicationName="/" />
      </providers>
    </profile>
    <membership defaultProvider="DefaultMembershipProvider">
      <providers>
        <add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxx" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
      </providers>
    </membership>
    <roleManager defaultProvider="DefaultRoleProvider">
      <providers>
        <add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxx" connectionStringName="DefaultConnection" applicationName="/" />
      </providers>
    </roleManager>
    <sessionState mode="InProc" customProvider="DefaultSessionProvider">
      <providers>
        <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxx" connectionStringName="DefaultConnection" />
      </providers>
    </sessionState>

I've seen number of answers on google including adding different role attributes in web.conf, so far no luck

can some one please help?

like image 595
kayze Avatar asked Oct 12 '13 00:10

kayze


3 Answers

By default the role manger is disabled, so you have to enable it explicitly:

<roleManager enabled="true" defaultProvider="DefaultRoleProvider">
like image 133
developer10214 Avatar answered Oct 27 '22 09:10

developer10214


I tried this in MVC 5 and it worked:

  <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <roleManager enabled="true" />
  </system.web>

After adding the "roleManager" line shown above to the web.config, I could add a new role and add a user to the role without any exception:

   if (!Roles.RoleExists(_role))
      Roles.CreateRole(_role);

   if (!Roles.IsUserInRole(_username, _role))
      Roles.AddUserToRole(_username, _role);
like image 37
AH. Avatar answered Oct 27 '22 10:10

AH.


With your DefaultRoleProvider try this

   <roleManager defaultProvider="DefaultRoleProvider" enabled ="true">
like image 35
Spock Avatar answered Oct 27 '22 10:10

Spock