The .NET Framework provides WindowsIdentity class that contains methods and properties for detecting the current windows user of the program. It contains the name of the current user, whether a user is authenticated, and the authentication type. There are also principal classes such as WindowsPrincipal and GenericPrincipal. Principals contain the identity and the roles a user belongs to. The following example shows you an example of using these classes.


using System;
using System.Security.Principal;
using System.Threading;
class Program
{

       
public static void Main()

       
{

               
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);


               
WindowsPrincipal myPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal;


               
WindowsIdentity myIdentity = (WindowsIdentity)myPrincipal.Identity;


               
Console.WriteLine("IdentityType: " + myIdentity.ToString());

               
Console.WriteLine("Name: {0}", myIdentity.Name);

               
Console.WriteLine("Member of Users? {0}",

                        myPrincipal
.IsInRole(WindowsBuiltInRole.User));

               
Console.WriteLine("Member of Administrators? {0}",

                        myPrincipal
.IsInRole(WindowsBuiltInRole.Administrator));

               
Console.WriteLine("Authenticated: {0}", myIdentity.IsAuthenticated);

               
Console.WriteLine("Anonymous: {0}", myIdentity.IsAnonymous);

       
}
}

Note that the output will vary for you because you might be using a different user.


IdentityType: System.Security.Principal.WindowsIdentity
Name: COMPUTERUsername
Member of Users? True
Member of Administrators? True
Authenticated: True
Anonymous: False


We first imported two namespaces: System.Security.Principal and System.Threading. The first line of code inside Main hooks up the principal with the current windows account. The second line of code gets the Identity from the role and stores it in a WindowsIdentity object. The third line uses the ToString() method to simply show the type of the Identity. The following codes uses the properties of Principal and Identity to show the details desires. Notice that we used the WindowsBuiltInRole enumeration as an argument for the IsInRole method of the Principal class. WindowsBuiltInRole enumeration contains the roles that are installed in your system by default such as the administrator role. 

Read more: http://forum.codecall.net/topic/58229-c-tutorial-obtaining-windows-user-details/#ixzz2GdjkmwyH

What Do You Think on This ? Say Here