Mono Class Library: System.Security.Principal Namespace

System.Security.Principal.WindowsImpersonationContext Class

This class holds the impersonation context and the possibility to revert to the original identity. [Edit]

See Also: WindowsImpersonationContext Members

[System.Runtime.InteropServices.ComVisible(true)]
public class WindowsImpersonationContext : IDisposable

Remarks

The WindowsImpersonationContext class can also be used on POSIX compliant operating system.
C# Example
using System;
using System.IO;
using System.Security.Principal;

class WinId {

     static string file = "root.txt";

     static void Show (string msg) 
     {
          Console.WriteLine (msg);
          WindowsIdentity wi = WindowsIdentity.GetCurrent ();
          Console.WriteLine ("\tUserName:\t{0}", wi.Name);
          Console.WriteLine ("\tToken:\t\t{0}", wi.Token);
          Console.WriteLine ();
     }

     static void Show (Exception e) 
     {
          Console.WriteLine ("Oops something has gone bad...");
          Console.WriteLine (e.ToString ());
     }

     static void DisplayFile (string filename) 
     {
          Console.WriteLine ("Trying to read file {0}", filename);
          try {
               using (StreamReader sr = File.OpenText (filename)) {
                    Console.WriteLine (sr.ReadToEnd ());
                    sr.Close ();
               }
          }
          catch (Exception e) {
               Show (e);
          }
     }

     static void DeleteFile (string filename) 
     {
          Console.WriteLine ("Trying to delete file {0}", filename);
          try {
               File.Delete (filename);
          }
          catch (Exception e) {
               Show (e);
          }
     }

     [STAThread]
     static void Main (string[] args)
     {
          bool fileCreated = false;

          Show ("Current User");
          bool posix = ((int)Environment.OSVersion.Platform == 128);
          bool root = (WindowsIdentity.GetCurrent ().Token == IntPtr.Zero);
          if (root && posix) {
               Console.WriteLine ("Cool you're root!");
               
               Console.WriteLine ("Let's create a file {0}.", file);
               using (StreamWriter sw = new StreamWriter (file)) {
                    sw.WriteLine ("FOR ROOT EYES ONLY");
                    sw.Close ();
               }
               fileCreated = true;
               DisplayFile (file);

               Console.WriteLine ("Now, who do you want to be today ? ");
               string username = Console.ReadLine ();
               WindowsIdentity nonroot = new WindowsIdentity (username);

               Console.WriteLine ("Trying to impersonate {0} (token: {1})",
                    nonroot.Name, nonroot.Token);
               WindowsImpersonationContext wic = null;
               try {
                    wic = nonroot.Impersonate ();
                    Show ("Impersonated User");
                    DisplayFile (file);
                    DeleteFile (file);
               }
               catch (Exception e) {
                    Show (e);
               }
               finally {
                    if (wic != null) {
                         wic.Undo ();
                         wic = null;
                    }
               }

               Show ("Back to original User");
               if (File.Exists (file)) {
                    DisplayFile (file);
                    DeleteFile (file);
               }
               else if (fileCreated) {
                    Console.WriteLine ("Hey dude, where's my file ?");
               }
          }
          else {
               Console.WriteLine ("Sorry, not much fun to have with non-root users.");
          }
     }
}  
[Edit]

Requirements

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