Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I retrieve all the roles (groups) a user is a member of?

Is there a way to get a list of roles a Windows authenticated user is in, without explicitly checking by WindowsPrincipal.IsInRole method?

like image 222
muratgu Avatar asked Apr 17 '09 21:04

muratgu


1 Answers

WindowsPrincipal.IsInRole just checks if the user is a member of the group with that name; a Windows Group is a Role. You can get a list of the groups that a user is a member of from the WindowsIdentity.Groups property.

You can get WindowsIdentity from your WindowsPrincipal:

WindowsIdentity identity = WindowsPrincipal.Identity as WindowsIdentity; 

or you can get it from a factory method on WindowsIdentity:

WindowsIdentity identity = WindowsIdentity.GetCurrent(); 

WindowsIdenity.Groups is a collection of IdentityReference which just gives you the SID of the group. If you need the group names you will need to translate the IdentityReference into an NTAccount and get the Value:

var groupNames = from id in identity.Groups                  select id.Translate(typeof(NTAccount)).Value; 
like image 62
joshperry Avatar answered Sep 28 '22 05:09

joshperry