Eric's Blog

Day to day experience in .NET
Welcome to Blogs @ IRM Sign in | Join | Help
 Search

Disclaimer

The content of this site is my own personal opinion and does not in any way represent my employer, it's subsideries or affiliates. These postings are provided "AS IS" with no warranties, and confer no rights.

This Blog

The Small Things: Getting the Names of the Users Windows Roles

UPDATE 2008-12-06: After Dominick Baier's comment (thanks!), I have now updated the code to be more effecient.

Since .NET 2.0 it has been possible to get the groups that a user belongs to, by accessing the Groups property of the WindowsIdentity class. What you get when from the property is an IdentityReference, which in turn has a Value property. You thought that the Value would contain the name of the group/role? No, it has the SID as the value, but this can be translated to the name, by using the Translate method. The translation can also be made for all the groups once by calling Translate on the IdentityReferenceCollection instead. This, of course, is much more effecient as Dominick comment on the original post.

WindowsIdentity identity = WindowsIdentity.GetCurrent();
IdentityReferenceCollection groups = identity.Groups.Translate(typeof(NTAccount));
foreach (IdentityReference group in groups)
{
    Debug.WriteLine(group.Value);
}
Published den 2 december 2008 10:48 by ericqu
Filed under: , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Dominick Baier said:

Hi, this is very inefficient if the account is a domain account. This way you would do one round-trip per group for name resolution. You can call Translate on the IdentityReferenceCollection (WindowsIdentity.Groups) to do resolution of all groups in one go. cheers dominick
december 6, 2008 14:50

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server, by Telligent Systems