Click here to Skip to main content
15,905,504 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm working with Roles for my asp.net webform application. I have all have RoleName already saved in database. What i want to achieve though, is that when a user logs in, i want to get the roleName of the user so as to use it for another thing(to query to Db for a specific result to be displayed.) i have tried to use


<pre>var userRole = Roles.GetRolesForUser(user.Value);

i get System.String[] returned back. which i cannot use to query to db.

What I have tried:

<pre lang="text">if (Membership.ValidateUser(user.Value, Password.Value))
      {
              var userRole = Roles.GetRolesForUser(user.Value);
     var GetSomeRecords = users.Where(m => m.Myrole == userRole).ToList();
        }
Posted
Comments
F-ES Sitecore 15-Jun-17 10:29am    
You get an array of string back because a person can have multiple rows. If your code needs one specific role then you need a way to query the roles returned to see if that role is in it, and if so use just that role. If you are using the roles as a "key" or lookup id for another table then your design is flawed, for the already mentioned issue that a user can have many roles.
Mcbaloo 15-Jun-17 10:34am    
Thanks for the comment. The role is not a key for another table. I just intend to use the roleName of the user to get a specific information from the database. Is there a way i can get the users roleNAME from the returned array of string. For the design, users can only belong to just one role.
Richard Deeming 15-Jun-17 11:35am    
Something like this?
string[] userRoles = Roles.GetRolesForUser(user.Value);
string userRole = userRoles.Length == 0 ? null : userRoles[0];
Mcbaloo 16-Jun-17 5:54am    
Thanks. With the above suggestion, i was able to get that i wanted. I worked

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900