Get your current WindowsIdentity name and groups

Are you having a little identity crisis? Not sure who you really are in a given environment? Or what groups you belong to? Find out for real, for sure. Get the name of the current WindowsIdentity (Windows user) and the groups to which the user belongs.

using System;
using System.Text;
using ssp = System.Security.Principal;

var user = ssp.WindowsIdentity.GetCurrent();
var name = user.Name;

var groups = new StringBuilder();
foreach ( var group in user.Groups )
  groups.AppendLine( group.Translate( typeof( ssp.NTAccount ) ).ToString() );

var message = string.Format( "Name: {0}\nGroups: {1}", name, groups.ToString() );

This nugget of code has saved me at least three times when I was convinced that I was someone else or in some other role/group than I really was.

Comments are closed.