posted on Thursday, June 29, 2006 2:12 PM
by
taylorza
Debugging WindowsIdentity and IsInRole 2.0
Update: Accidentally posted an older version of the function, this is a slightly cleaner version.
In a previous post I showed how to get get a list of groups from a WindowsIdentity, this method used all kinds of under handed trick to get to the information and as warned the solution no longer works for the new version of the framework. Here is an updated version which takes advantage of officially supported techniques available in the 2.0 Framework, given a WindowsIdentity it returns a list of role names from the WindowsIdentity.
public
string[] GetWindowsIdentityRoles(WindowsIdentity identity)
{
if (identity == null) throw new ArgumentNullException("identity");
IdentityReferenceCollection groups = identity.Groups.Translate(typeof(NTAccount));
string[] roles = new string[groups.Count];
for (int i = 0; i < groups.Count; ++i)
{
roles[i] = groups[i].Value;
}
return roles;
}
This function comes in handy when you are not certain of the string representation of a role you need to pass to the IsInRole function.