3

Is there any way to get RoleId without get directly from DB?, I know we can get role Names by:

string[] allRoles = System.Web.Security.Roles.GetAllRoles();
string[] allRolesForUser = System.Web.Security.Roles.GetRolesForUser(httpContext.User.Identity.Name);

But I need to access roleId.

Does any one have any idea about it?

Saeid
  • 13,224
  • 32
  • 107
  • 173
  • The Role API doesn't provide a way to get role IDs. I suppose you could create a custom role provider. – Tuan Apr 03 '12 at 05:59

4 Answers4

2

No. The role provider have no knowledge about the data source. It can be a very slow web service or a super deluxe NoSQL database. The provider doesn't know that your db as a primary key.

Sure. The SqlMembershipProvider does. But having it exposing the key would likely lead to violations against Liskovs Substitution Principle.

The role name should be unique. So you should yourself be able to fetch it from the database. Or why can`t you simply use the role name directly instead of the db key?

Community
  • 1
  • 1
jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • ASP.NET Role Manager has a bug. Though doesn't allow duplicate role, if role has white space in-between and put different white-spaces for different roles, they are allowed, finally being multiple roles with same name ! – Altaf Patel Jun 12 '13 at 11:51
  • the bug is that you allow white spaces. – jgauffin Jun 12 '13 at 13:25
  • but .NET should truncate multiple to single whitespace and validate ! – Altaf Patel Jun 18 '13 at 06:18
  • why? It's your business rule, not the membership providers. Or is that rule listed anywhere in the documentation? – jgauffin Jun 18 '13 at 09:46
  • it's .NET validation that prevents duplicate roles, why not in that case ? – Altaf Patel Jun 21 '13 at 06:13
  • Because they are not duplicates. You have different characters in them. – jgauffin Jun 21 '13 at 08:57
  • then why .NET prevents duplicate role names, whereas I need to have different characters in them though with the same name. – Altaf Patel Jun 21 '13 at 09:41
  • I'll tell you why --- because the .NET role/user provider is a pile of junk. It's extremely limited in what it allows you to do. My advice, don't use it if you're looking for anything more than a bare-bones ability to create users and roles and associate them together. – Jagd May 22 '14 at 20:37
2

You must add aspnet_Roles table to your model and use query (for example LINQ) to get roleId .You can change MembershipProvider but it need more work for doing it.

Shayan
  • 402
  • 1
  • 4
  • 18
0

I realize this is a few years old, but when i bumped into it I saw that noone actually answers completely answers the question ...so I thought I would post a full solution.

Soooo...

Simply put:

SELECT RoleId, RoleName FROM aspnet_Roles;
GO

But for getting RoleIds for a User it is like this:

SELECT UR.RoleID, R.RoleName FROM
aspnet_Users U, aspnet_Roles R, aspnet_UsersInRoles UR 
WHERE U.UserName = @Username 
AND UR.UserId = U.UserId 
AND UR.RoleID = R.RoleId
GO

This will give you a 2 column list of RoleIds and RoleNames for a particular user.

In reality, you should not be trying to do this as there is a potential for Security breach when a RoleId is exposed. You should only work with RoleNames and use the Membership and Roles methods to manage things.

Hope this helps :)

Art
  • 1
0

You can't. ASP.NET MVC doesn't allow to get RoleId with standard functon, you must get it from database with the help of role name.

Chuck Norris
  • 15,207
  • 15
  • 92
  • 123