0

I have created the following class for customized roleProvider in a folder called providers within my web project:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Project.Providers
{
  public class MyCustomRoleProvider : System.Web.Security.SqlRoleProvider
  {
    public override string[] GetRolesForUser(string username)
    {
        string[] currentUserRoles = { "Admin", "User" };
        return currentUserRoles;
    }
  }
}

I have added the following section in the web.config:

<roleManager enabled="true" defaultProvider="DefaultRoleProvider">
    <providers>
      <add name="DefaultRoleProvider" type="Project.Provider.MyCustomRoleProvider, MetaLearning"/>
    </providers>
  </roleManager>

When I try to add the following code on my home controller:

Roles.AddUserToRole("username","role");

I am receiving an error that The Role Manager feature has not been enabled. What am I doing wrong?

Jay
  • 3,012
  • 14
  • 48
  • 99
  • Have you tried putting before your tag, The clear element is used to clear all the providers stored for this application earlier, for example the default providers. – Erwin Aug 23 '13 at 06:55
  • yes I have used the clear element but this still telling me that the role manager feature is not enabled – Jay Aug 23 '13 at 10:10
  • I've tried the same thing you did and it worked, I had to give a connectionStringName property in the provider add though. Only when I comment out the complete roleManager part I get the same error. Are yo sure it is in the system.web tag? Are you sure the web.config is being used? – Erwin Aug 23 '13 at 11:06
  • What connectionStringName did you have to add for the provider? – Jay Aug 23 '13 at 11:32
  • When I leave out the connectionStringName I get a configurationerror at runtime, if you don't get that my guess is your rolemanger config part isn't getting picked up at all ... the connectionstringname in my case was one pointing to an aspnetdb db. Could you doublecheck or post your complete config. – Erwin Aug 23 '13 at 11:45
  • I noticed that my project namespace was wrong and when I fixed this I am now getting the The attribute 'connectionStringName' is missing or empty. error as described. Could you show me how you fixed your problem? – Jay Aug 23 '13 at 13:09
  • It wasn't fixing, I had a DB Server with an aspnetdb database on in, I made a connectionstring pointing to that db and used it in the connectionStringName. For more info about the aspnetdb look here http://msdn.microsoft.com/en-us/library/x28wfk74%28v=vs.100%29.aspx – Erwin Aug 23 '13 at 13:56
  • I am using ADFS and claims for the login and so this was not a good solution for me, I used the custom provider and override the methods against my entity framework dbcontext and this is now working as expected. – Jay Aug 23 '13 at 14:11

1 Answers1

-1

Sometimes there are two web.config file in our project.Like in MVC4 internet application project one belongs in views folder This is not the configuration file for your application. It contains the configuration required to make views work with ASP.NET. Make sure you implement your requirements in correct web.config file.

Arun Thankur
  • 129
  • 1
  • 1
  • 5