Mono Class Library: System.Security.Principal Namespace

System.Security.Principal.WindowsPrincipal Class

This class represents the group membership of an operating system user. [Edit]

See Also: WindowsPrincipal Members

[System.Runtime.InteropServices.ComVisible(true)]
public class WindowsPrincipal : IPrincipal

Remarks

The WindowsPrincipal class can also be used on POSIX compliant operating system.

C# Example
using System;
using System.Runtime.InteropServices;
using System.Security.Principal;

class WinPrincipal {

     [DllImport ("advapi32.dll", CallingConvention=CallingConvention.StdCall)]
     public static extern bool LogonUser (string lpszUsername, 
          string lpszDomain, string lpszPassword, int dwLogonType,
          int dwLogonProvider, ref IntPtr phToken);

     static void Principal (WindowsIdentity user) 
     {
          WindowsPrincipal wp = new WindowsPrincipal (user);
          while (true) {
               Console.Write ("Enter group name: ");
               string group = Console.ReadLine ();
               if (group == String.Empty)
                    break;

               Console.WriteLine ("\t{0} is {1}a member of {2}{3}", 
                    user.Name,
                    (wp.IsInRole (group) ? String.Empty : "NOT "),
                    group,
                    Environment.NewLine);
          }
     }

     [STAThread]
     static void Main (string[] args)
     {
          bool posix = ((int) Environment.OSVersion.Platform == 128);
          while (true) {
               Console.Write ("Enter username:\t");
               string username = Console.ReadLine ();
               if (username == String.Empty)
                    break;

               WindowsIdentity user = null;
               // check for Posix versus Windows
               if (posix) {
                    user = new WindowsIdentity (username);
               }
               else {
                    Console.Write ("Enter domain:\t");
                    string domain = Console.ReadLine ();
                    Console.Write ("Enter password:\t");
                    string password = Console.ReadLine ();

                    IntPtr token = IntPtr.Zero;
                    // note: we should close that handle
                    LogonUser (username, domain, password, 2, 0, ref token);
                    if (token == IntPtr.Zero) {
                         Console.WriteLine ("Invalid username/domain/password");
                         continue;
                    }

                    user = new WindowsIdentity (token);
               }

               Principal (user);
          }
     }
}  
[Edit]

Requirements

Namespace: System.Security.Principal
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0