1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
<Type Name="WindowsImpersonationContext" FullName="System.Security.Principal.WindowsImpersonationContext">
<TypeSignature Maintainer="auto" Language="C#" Value="public class WindowsImpersonationContext : IDisposable" />
<AssemblyInfo>
<AssemblyName>mscorlib</AssemblyName>
<AssemblyPublicKey>[00 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00]</AssemblyPublicKey>
<AssemblyVersion>1.0.5000.0</AssemblyVersion>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ThreadSafetyStatement>Gtk# is thread aware, but not thread safe; See the <link location="node:gtk-sharp/programming/threads">Gtk# Thread Programming</link> for details.</ThreadSafetyStatement>
<Base>
<BaseTypeName>System.Object</BaseTypeName>
</Base>
<Interfaces>
<Interface>
<InterfaceName>System.IDisposable</InterfaceName>
</Interface>
</Interfaces>
<Docs>
<summary>This class holds the impersonation context and the possibility to revert to the original identity.</summary>
<remarks>The WindowsImpersonationContext class can also be used on POSIX compliant operating system.
<example><code lang="C#">
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.");
}
}
} </code></example></remarks>
</Docs>
<Members>
<Member MemberName="Dispose">
<MemberSignature Language="C#" Value="public void Dispose ();" />
<MemberType>Method</MemberType>
<ReturnValue>
<ReturnType>System.Void</ReturnType>
</ReturnValue>
<Parameters />
<Docs>
<summary>To be added.</summary>
<remarks>To be added.</remarks>
<since version=".NET 2.0" />
</Docs>
<AssemblyInfo>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Attributes>
<Attribute>
<AttributeName>System.Runtime.InteropServices.ComVisible(false)</AttributeName>
</Attribute>
</Attributes>
</Member>
<Member MemberName="Dispose">
<MemberSignature Language="C#" Value="protected virtual void Dispose (bool disposing);" />
<MemberType>Method</MemberType>
<ReturnValue>
<ReturnType>System.Void</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="disposing" Type="System.Boolean" />
</Parameters>
<Docs>
<param name="disposing">To be added.</param>
<summary>To be added.</summary>
<remarks>To be added.</remarks>
<since version=".NET 2.0" />
</Docs>
<AssemblyInfo>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Attributes>
<Attribute>
<AttributeName>System.Runtime.InteropServices.ComVisible(false)</AttributeName>
</Attribute>
</Attributes>
</Member>
<Member MemberName="Finalize">
<MemberSignature Language="C#" Value="~WindowsImpersonationContext ();" />
<MemberType>Method</MemberType>
<ReturnValue>
<ReturnType>System.Void</ReturnType>
</ReturnValue>
<Parameters />
<Docs>
<summary>To be added.</summary>
<remarks>To be added.</remarks>
</Docs>
<AssemblyInfo>
<AssemblyVersion>1.0.5000.0</AssemblyVersion>
</AssemblyInfo>
</Member>
<Member MemberName="Undo">
<MemberSignature Language="C#" Value="public void Undo ();" />
<MemberType>Method</MemberType>
<ReturnValue>
<ReturnType>System.Void</ReturnType>
</ReturnValue>
<Parameters />
<Docs>
<summary>Revert to the previous identity.</summary>
<remarks>To be added</remarks>
</Docs>
<AssemblyInfo>
<AssemblyVersion>1.0.5000.0</AssemblyVersion>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
</Member>
</Members>
<Attributes>
<Attribute>
<AttributeName>System.Runtime.InteropServices.ComVisible(true)</AttributeName>
</Attribute>
</Attributes>
</Type>
|