Allow roles not working but allow users working in web.config

.net, asp.net, asp.net-membership, forms-authentication, web-config

Solution

`RoleProvider` use `GetRolesForUser` method to retrieves authorized roles for a user.

// Notice that it accept username instead of email
public string[] GetRolesForUser(string username)

In your case, your `RoleProvider` passes `master@yahoo.com` as username. As the result, there is no username with `master@yahoo.com` in User table.

Here are solutions -

1) The easiest way to solve the problem is copy Email column of Memberships table to UserName column of Users table.

2) The hard way create custom `RoleProvider` and override `GetRolesForUser` method.

3) If you happen to use `legacy Membership Provider` instead of Universal Membership Provider, you can modify aspnet_UsersInRoles_GetRolesForUser.

Problem

I implemented asp.net membership for forms authentication. Generally for login it is used username, but i changed this to login with email id. Which is working fine. Now i am trying to restrict access to the users which are not in Admin role. I created web.config file in that folder i written like this. ``` <?xml version="1.0"?> <configuration> <system.web> <authorization> <allow roles="Admin" /> <deny users="*"/> </authorization> </system.web> </configuration> ``` When i tested with this one even though the users who is having the admin role is not able to access. When he clicks the link he is signing out (like the user doesn't have permission to access the page). For showing menu i am using web.sitemap file. It is working fine based on roles. Code in the web.cofnig file under that particular folder is correct as per MSDN. I don't know why it is not working. If we change membership controls default login behavior then any thing should take care about roles? Many questions to similar were found in stackoverflow as well as in some other sites also. But nothing gives me solution. Whats wrong in this? Need to write any custom code to handle? For allowing users i written like below. It is working perfect ``` <?xml version="1.0"?> <configuration> <system.web> <authorization> <allow users="master@yahoo.com" /> <deny users="*"/> </authorization> </system.web> </configuration> ``` Why it is not working only for roles?

Original source