[RESOLVED]Issue returning all roles SimpleMembership


I have created two roles Admin, Dealer

I am using Simple Member Ship inside my MVC 4 Application, when the user logs in I need to figure out if he/she is an admin or a dealer below is the code I currently have

  var roles = new List<string> { "Admin", "Author", "Super" };
            var userRoles = Roles.GetRolesForUser(User.Identity.Name);

            if (userRoles.Any(u => roles.Contains(u)))
                //do something

but I get an error on

var userRoles = Roles.GetRolesForUser(User.Identity.Name);

which is 

An exception of type ‘System.Web.HttpException’ occurred in System.Web.dll but was not handled in user code

Additional information: Unable to connect to SQL Server database.

Yet I can register new users and log in….

how come it works for one thing but doesn’t for another am I missing something?

There two configurations elements in web.config one for membership (users) another for rolemanager(roles)

two elements should have same connection string name

As you are saying roles already been created, they might be created in default provider’s database.

<roleManager enabled="true">
         connectionStringName="connectionstringnamewhich you give to memebrship" 
         type="System.Web.Security.SqlRoleProvider, System.Web, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />


Hi Harrison,

I think 

if (userRoles.Any(u => roles.Contains(u))

this line of code is causing the problem.

What do you want to check with  this??

Is this what you intend to do?

if (userRoles.Any(u => roles.Contains("Admin")))

Hope This Helps

Leave a Reply