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?
By default the role manger is disabled, so you have to enable it explicitly:
<roleManager enabled="true" defaultProvider="DefaultRoleProvider">
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);
With your DefaultRoleProvider try this
<roleManager defaultProvider="DefaultRoleProvider" enabled ="true">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With