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: COMPUTER\Username 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.


Sign In
Create Account


Back to top











