(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Determining group identity: WindowsBuiltInRole.PowerUser
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");
}
}
}
You are not a member of the Power User group
Get current principal identity name
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.");
}
}
}
Get the current identity
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);
}
}
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
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);
}
}
Principal information:
Authentication Type: NTLM
Is authenticated: True
Name: nfex\Joe
Impersonation
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();
}
}
Iterate through the group names to see if the current user is a member of each one
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));
}
}
}
Obtain a WindowsIdentity object representing the currently logged on Windows user
using System;
using System.Security.Principal;
class MainClass
{
public static void Main (string[] args)
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
}
}