Csharp/C Sharp/Windows/Windows Principal — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 11:42, 26 мая 2010
Содержание
Get Current Windows Identity
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));
}
}
}
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);
}
}
WindowsBuiltInRole.Administrator
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.");
}
}
WindowsImpersonationContext
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);
}
}
}
WindowsPrincipal Enables You to Check for Role Membership
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.");
}
}
WindowsPrincipal.IsInRole
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));
}
}
}