June 2006 - Posts

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.

Gradient Shades

Recently I was googling and came a routine to calculate the transision from one color to another color in 255 discreet steps.
Looking at the code it did not quite look like it was going to achieve what I understood goal of the routine to be. So I quickly
put some code together and then found that there was no way to post a comment on the blog. The original post can be found
here http://dalldorf.us/home/?m=200603

So rather than waste the code here it is.

public Color Gradient(Color from, Color to, byte step)
{
   double stepFactor = (double)step / 255;
   
return Color.FromArgb(
      (
int)(from.R + ((to.R - from.R) * stepFactor)),
      (
int)(from.G + ((to.G - from.G) * stepFactor)),
      (
int)(from.B + ((to.B - from.B) * stepFactor)));
}