Get current principal identity name : WindowsIdentity « Security « C# / CSharp Tutorial






using System;
using System.Threading;
using System.Security;
using System.Security.Permissions;
using System.Collections.Generic;
using System.Text;
using System.Collections;

    class Program
    {
        static void Main(string[] args)
        {          
            System.Security.Principal.WindowsIdentity wi = System.Security.Principal.WindowsIdentity.GetCurrent();
            Thread.CurrentPrincipal = new System.Security.Principal.WindowsPrincipal(wi);
            Console.WriteLine(wi.Name);
            Console.WriteLine(Thread.CurrentPrincipal.Identity.Name);
            PrincipalPermission pp = new PrincipalPermission(null, "Administrators", true);
            pp.Demand();

            PrincipalPermission pp2 = new PrincipalPermission(null, "Users", true);
            pp.Union(pp2).Demand();

            try
            {
                PrincipalPermission pp3 = new PrincipalPermission(null, "Club");
                pp3.Demand();
            }
            catch (SecurityException e)
            {
                Console.WriteLine("You do not have access to the secret club.");
            }
        }
    }








35.1.WindowsIdentity
35.1.1.Obtain a WindowsIdentity object representing the currently logged on Windows user
35.1.2.Get the current identity
35.1.3.Get the current identity and its associated principal
35.1.4.Determining group identity: WindowsBuiltInRole.PowerUser
35.1.5.Iterate through the group names to see if the current user is a member of each one
35.1.6.Impersonation
35.1.7.Get current principal identity name