C# – how to get Roles associated with a user from asp.net FormsAuthentication object

asp.netcforms-authenticationroles

How to get Roles associated with a user from asp.net FormsAuthentication object? Other than IsInRole I don't see any methods useful
in this respect. I am looking for something like GetRoles(). All I usee is User.IsInRole("rolename") and I need to loop through
my roles to find my role of iterest.
is there a way to get roles from Formsauthticket itself?

Best Answer

Actually the code to get the Role of the user can be this one:

Roles.GetRolesForUser(WebSecurity.CurrentUserName);

I am providing you with the WebSecurity.CurrentUserName to get the username for the current user.

You can try to use something else like:

Roles.GetRolesForUser("afzaal_ahmad_zeeshan")

Where afzaal_ahmad_zeeshan is the username of the user of whom you want to get the Role.

Also note that this provides you with a String[] array so using a foreach () loop might be the solution to filter out the result.

For More: http://msdn.microsoft.com/en-us/library/8h930x07.aspx