Csharp/CSharp Tutorial/Security/WindowsIdentity
Содержание
- 1 Determining group identity: WindowsBuiltInRole.PowerUser
- 2 Get current principal identity name
- 3 Get the current identity
- 4 Get the current identity and its associated principal
- 5 Impersonation
- 6 Iterate through the group names to see if the current user is a member of each one
- 7 Obtain a WindowsIdentity object representing the currently logged on Windows user
Determining group identity: WindowsBuiltInRole.PowerUser
<source lang="csharp">using System; using System.Security.Principal; class MainClass {
public static void Main() { WindowsIdentity wi = WindowsIdentity.GetCurrent(); WindowsPrincipal prin = new WindowsPrincipal(wi); if (prin.IsInRole(WindowsBuiltInRole.PowerUser)) { Console.WriteLine("You are a member of the Power User group"); } else { Console.WriteLine("You are not a member of the Power User group"); } }
}</source>
You are not a member of the Power User group
Get current principal identity name
<source lang="csharp">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."); } } }</source>
Get the current identity
<source lang="csharp">using System; using System.Security.Principal;
class MainClass {
public static void Main() { WindowsIdentity wi = WindowsIdentity.GetCurrent(); Console.WriteLine("Identity information:"); Console.WriteLine(" Authentication Type: {0}",wi.AuthenticationType); Console.WriteLine(" Is Anonymous: {0}", wi.IsAnonymous); Console.WriteLine(" Is Authenticated: {0}", wi.IsAuthenticated); Console.WriteLine(" Is Guest: {0}", wi.IsGuest); Console.WriteLine(" Is System: {0}", wi.IsSystem); Console.WriteLine(" Name: {0}", wi.Name); Console.WriteLine(" Token: {0}", wi.Token); }
}</source>
Identity information: Authentication Type: NTLM Is Anonymous: False Is Authenticated: True Is Guest: False Is System: False Name: nfex\Joe Token: 1800
Get the current identity and its associated principal
<source lang="csharp">using System; using System.Security.Principal;
class MainClass {
public static void Main() { WindowsIdentity wi = WindowsIdentity.GetCurrent(); WindowsPrincipal prin = new WindowsPrincipal(wi); Console.WriteLine("Principal information:"); Console.WriteLine(" Authentication Type: {0}", prin.Identity.AuthenticationType); Console.WriteLine(" Is authenticated: {0}", prin.Identity.IsAuthenticated); Console.WriteLine(" Name: {0}", prin.Identity.Name); }
}</source>
Principal information: Authentication Type: NTLM Is authenticated: True Name: nfex\Joe
Impersonation
<source lang="csharp">using System; using System.Collections.Generic; using System.IO; using System.IO.IsolatedStorage; using System.Net; using System.Net.Sockets; using System.Reflection; using System.Security; using System.Security.AccessControl; using System.Security.Policy; using System.Security.Permissions; using System.Security.Principal; using System.Text; public class MainClass {
public static void Main() { WindowsIdentity identity = WindowsIdentity.GetCurrent(); WindowsImpersonationContext context = identity.Impersonate(); context.Undo(); }
}</source>
Iterate through the group names to see if the current user is a member of each one
<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>
Obtain a WindowsIdentity object representing the currently logged on Windows user
<source lang="csharp">using System; using System.Security.Principal; class MainClass {
public static void Main (string[] args) { WindowsIdentity identity = WindowsIdentity.GetCurrent(); }
}</source>