Csharp/C Sharp/Windows/Windows Principal
Содержание
Get Current Windows Identity
<source lang="csharp"> using System; using System.Security.Principal; class MainClass {
public static void Main(string[] args) { WindowsIdentity identity = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(identity); foreach (string role in args) { Console.WriteLine("Is {0} a member of {1}? = {2}", identity.Name, role, principal.IsInRole(role)); } }
}
</source>
Use Properties of WindowsPrincipal
<source lang="csharp"> 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); }
}
</source>
WindowsBuiltInRole.Administrator
<source lang="csharp"> using System; using System.Security.Principal; class Class1 {
static void Main() { WindowsIdentity wi = WindowsIdentity.GetCurrent(); WindowsPrincipal wp = new WindowsPrincipal(wi); if (wp.IsInRole(WindowsBuiltInRole.Administrator)) Console.WriteLine("Your are an Administrator!"); else Console.WriteLine("You are not an Administrator."); if (wp.IsInRole("POWERHOUSE\\Developer")) Console.WriteLine("You are in the Developer group!"); else Console.WriteLine("You are not in the Developer group."); }
}
</source>
WindowsImpersonationContext
<source lang="csharp"> using System; using System.IO; using System.Security.Principal; using System.Security.Permissions; using System.Runtime.InteropServices; [assembly: SecurityPermission(SecurityAction.RequestMinimum, UnmanagedCode = true, ControlPrincipal = true)] class MainClass {
const int LOGON32_PROVIDER_DEFAULT = 0; const int LOGON32_LOGON_INTERACTIVE = 2; [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)] static extern bool LogonUser(string userName, string domain, string password, int logonType, int logonProvider, ref IntPtr accessToken); public static void Main(string[] args) { IntPtr accessToken = IntPtr.Zero; bool success = LogonUser( args[0], // username to log on. ".", // use the local account database. args[1], // user"s password. LOGON32_LOGON_INTERACTIVE, // create an interactive login. LOGON32_PROVIDER_DEFAULT, // use the default logon provider. ref accessToken // receives access token handle. ); if (!success) { Console.WriteLine("LogonUser returned error {0}", Marshal.GetLastWin32Error()); } else { WindowsIdentity identity = new WindowsIdentity(accessToken); Console.WriteLine(WindowsIdentity.GetCurrent().Name); WindowsImpersonationContext impContext = identity.Impersonate(); Console.WriteLine(WindowsIdentity.GetCurrent().Name); impContext.Undo(); Console.WriteLine(WindowsIdentity.GetCurrent().Name); } }
}
</source>
WindowsPrincipal Enables You to Check for Role Membership
<source lang="csharp"> using System; using System.Security.Principal;
class Class1 {
static void Main() { WindowsIdentity wi = WindowsIdentity.GetCurrent(); WindowsPrincipal wp = new WindowsPrincipal(wi); // This checks for local administrator rights if you in a Domain if (wp.IsInRole(WindowsBuiltInRole.Administrator)) Console.WriteLine("Your are an Administrator!"); else Console.WriteLine("You are not an Administrator."); if (wp.IsInRole("YourRole\\Developer")) Console.WriteLine("You are in the Developer group!"); else Console.WriteLine("You are not in the Developer group."); }
}
</source>
WindowsPrincipal.IsInRole
<source lang="csharp">
using System; using System.Security.Principal; class MainClass {
public static void Main(string[] args) { WindowsIdentity identity = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(identity); foreach (string role in args) { Console.WriteLine("Is {0} a member of {1}? = {2}", identity.Name, role, principal.IsInRole(role)); } }
}
</source>