<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FC_Sharp%2FWindows%2FWindows_Principal</id>
		<title>Csharp/C Sharp/Windows/Windows Principal - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FC_Sharp%2FWindows%2FWindows_Principal"/>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/C_Sharp/Windows/Windows_Principal&amp;action=history"/>
		<updated>2026-04-30T01:11:13Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/C_Sharp/Windows/Windows_Principal&amp;diff=1087&amp;oldid=prev</id>
		<title> в 15:31, 26 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/C_Sharp/Windows/Windows_Principal&amp;diff=1087&amp;oldid=prev"/>
				<updated>2010-05-26T15:31:19Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 15:31, 26 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/C_Sharp/Windows/Windows_Principal&amp;diff=1088&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/C_Sharp/Windows/Windows_Principal&amp;diff=1088&amp;oldid=prev"/>
				<updated>2010-05-26T11:42:58Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Get Current Windows Identity==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Security.Principal;&lt;br /&gt;
class MainClass {&lt;br /&gt;
    public static void Main(string[] args) {&lt;br /&gt;
        WindowsIdentity identity = WindowsIdentity.GetCurrent();&lt;br /&gt;
        WindowsPrincipal principal = new WindowsPrincipal(identity);&lt;br /&gt;
        foreach (string role in args) {&lt;br /&gt;
            Console.WriteLine(&amp;quot;Is {0} a member of {1}? = {2}&amp;quot;,&lt;br /&gt;
             identity.Name, role, principal.IsInRole(role));&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Use Properties of WindowsPrincipal==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Security.Principal;&lt;br /&gt;
using System.Security.Permissions;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
class Program {&lt;br /&gt;
    static void Main(string[] args) {&lt;br /&gt;
        AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);&lt;br /&gt;
        WindowsPrincipal principal = (WindowsPrincipal)Thread.CurrentPrincipal;&lt;br /&gt;
        WindowsIdentity identity = (WindowsIdentity)principal.Identity;&lt;br /&gt;
        Console.WriteLine(&amp;quot;IdentityType: &amp;quot; + identity.ToString());&lt;br /&gt;
        Console.WriteLine(&amp;quot;Name: &amp;quot; + identity.Name);&lt;br /&gt;
        Console.WriteLine(&amp;quot;&amp;quot;Users&amp;quot;?: &amp;quot; + principal.IsInRole(&amp;quot;BUILTIN\\Users&amp;quot;));&lt;br /&gt;
        Console.WriteLine(&amp;quot;&amp;quot;Administrators&amp;quot;?: &amp;quot; + principal.IsInRole(WindowsBuiltInRole.Administrator));&lt;br /&gt;
        Console.WriteLine(&amp;quot;Authenticated: &amp;quot; + identity.IsAuthenticated);&lt;br /&gt;
        Console.WriteLine(&amp;quot;AuthType: &amp;quot; + identity.AuthenticationType);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Anonymous?: &amp;quot; + identity.IsAnonymous);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Token: &amp;quot; + identity.Token);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==WindowsBuiltInRole.Administrator==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Security.Principal;&lt;br /&gt;
class Class1&lt;br /&gt;
{&lt;br /&gt;
    static void Main()&lt;br /&gt;
  {   &lt;br /&gt;
    WindowsIdentity wi = WindowsIdentity.GetCurrent();&lt;br /&gt;
    WindowsPrincipal wp = new WindowsPrincipal(wi);&lt;br /&gt;
        if (wp.IsInRole(WindowsBuiltInRole.Administrator))&lt;br /&gt;
      Console.WriteLine(&amp;quot;Your are an Administrator!&amp;quot;);&lt;br /&gt;
    else&lt;br /&gt;
      Console.WriteLine(&amp;quot;You are not an Administrator.&amp;quot;);&lt;br /&gt;
    if (wp.IsInRole(&amp;quot;POWERHOUSE\\Developer&amp;quot;))&lt;br /&gt;
      Console.WriteLine(&amp;quot;You are in the Developer group!&amp;quot;);&lt;br /&gt;
    else&lt;br /&gt;
      Console.WriteLine(&amp;quot;You are not in the Developer group.&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==WindowsImpersonationContext==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.IO;&lt;br /&gt;
using System.Security.Principal;&lt;br /&gt;
using System.Security.Permissions;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
[assembly: SecurityPermission(SecurityAction.RequestMinimum, UnmanagedCode = true, ControlPrincipal = true)]&lt;br /&gt;
class MainClass {&lt;br /&gt;
    const int LOGON32_PROVIDER_DEFAULT = 0;&lt;br /&gt;
    const int LOGON32_LOGON_INTERACTIVE = 2;&lt;br /&gt;
    [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Unicode)]&lt;br /&gt;
    static extern bool LogonUser(string userName, string domain,&lt;br /&gt;
        string password, int logonType, int logonProvider,&lt;br /&gt;
        ref IntPtr accessToken);&lt;br /&gt;
    public static void Main(string[] args) {&lt;br /&gt;
        IntPtr accessToken = IntPtr.Zero;&lt;br /&gt;
        bool success = LogonUser(&lt;br /&gt;
            args[0],                    // username to log on.&lt;br /&gt;
            &amp;quot;.&amp;quot;,                         // use the local account database.&lt;br /&gt;
            args[1],                    // user&amp;quot;s password.&lt;br /&gt;
            LOGON32_LOGON_INTERACTIVE,  // create an interactive login.&lt;br /&gt;
            LOGON32_PROVIDER_DEFAULT,    // use the default logon provider.&lt;br /&gt;
            ref accessToken             // receives access token handle.&lt;br /&gt;
        );&lt;br /&gt;
        if (!success) {&lt;br /&gt;
            Console.WriteLine(&amp;quot;LogonUser returned error {0}&amp;quot;,&lt;br /&gt;
               Marshal.GetLastWin32Error());&lt;br /&gt;
        } else {&lt;br /&gt;
            WindowsIdentity identity = new WindowsIdentity(accessToken);&lt;br /&gt;
            Console.WriteLine(WindowsIdentity.GetCurrent().Name);&lt;br /&gt;
            WindowsImpersonationContext impContext = identity.Impersonate();&lt;br /&gt;
            Console.WriteLine(WindowsIdentity.GetCurrent().Name);&lt;br /&gt;
            impContext.Undo();&lt;br /&gt;
            Console.WriteLine(WindowsIdentity.GetCurrent().Name);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==WindowsPrincipal Enables You to Check for Role Membership==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Security.Principal;&lt;br /&gt;
   &lt;br /&gt;
class Class1 {&lt;br /&gt;
    static void Main() {   &lt;br /&gt;
       WindowsIdentity wi = WindowsIdentity.GetCurrent();&lt;br /&gt;
       WindowsPrincipal wp = new WindowsPrincipal(wi);&lt;br /&gt;
   &lt;br /&gt;
       // This checks for local administrator rights if you in a Domain&lt;br /&gt;
       if (wp.IsInRole(WindowsBuiltInRole.Administrator))&lt;br /&gt;
           Console.WriteLine(&amp;quot;Your are an Administrator!&amp;quot;);&lt;br /&gt;
       else&lt;br /&gt;
           Console.WriteLine(&amp;quot;You are not an Administrator.&amp;quot;);&lt;br /&gt;
   &lt;br /&gt;
       if (wp.IsInRole(&amp;quot;YourRole\\Developer&amp;quot;))&lt;br /&gt;
           Console.WriteLine(&amp;quot;You are in the Developer group!&amp;quot;);&lt;br /&gt;
       else&lt;br /&gt;
           Console.WriteLine(&amp;quot;You are not in the Developer group.&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==WindowsPrincipal.IsInRole==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
using System;&lt;br /&gt;
using System.Security.Principal;&lt;br /&gt;
class MainClass {&lt;br /&gt;
    public static void Main(string[] args) {&lt;br /&gt;
        WindowsIdentity identity = WindowsIdentity.GetCurrent();&lt;br /&gt;
        WindowsPrincipal principal = new WindowsPrincipal(identity);&lt;br /&gt;
        foreach (string role in args) {&lt;br /&gt;
            Console.WriteLine(&amp;quot;Is {0} a member of {1}? = {2}&amp;quot;,&lt;br /&gt;
             identity.Name, role, principal.IsInRole(role));&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>