Use Properties of WindowsPrincipal : Windows Principal « Windows « C# / C Sharp






Use Properties of WindowsPrincipal


using System;
using System.Security.Principal;
using System.Security.Permissions;
using System.Threading;

class Program {
    static void Main(string[] args) {
        AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
        WindowsPrincipal principal = (WindowsPrincipal)Thread.CurrentPrincipal;
        WindowsIdentity identity = (WindowsIdentity)principal.Identity;
        Console.WriteLine("IdentityType: " + identity.ToString());
        Console.WriteLine("Name: " + identity.Name);
        Console.WriteLine("'Users'?: " + principal.IsInRole("BUILTIN\\Users"));
        Console.WriteLine("'Administrators'?: " + principal.IsInRole(WindowsBuiltInRole.Administrator));
        Console.WriteLine("Authenticated: " + identity.IsAuthenticated);
        Console.WriteLine("AuthType: " + identity.AuthenticationType);
        Console.WriteLine("Anonymous?: " + identity.IsAnonymous);
        Console.WriteLine("Token: " + identity.Token);
    }
}
           
       








Related examples in the same category

1.WindowsPrincipal.IsInRole
2.WindowsImpersonationContext
3.Get Current Windows Identity
4.WindowsBuiltInRole.Administrator
5.WindowsPrincipal Enables You to Check for Role MembershipWindowsPrincipal Enables You to Check for Role Membership