2 // System.Security.Principal.WindowsIdentity
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 // Sebastien Pouliot (sebastien@ximian.com)
8 // (C) 2002 Ximian, Inc (http://www.ximian.com)
9 // Portions (C) 2003 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Runtime.CompilerServices;
33 using System.Runtime.InteropServices;
34 using System.Runtime.Serialization;
35 using System.Security.Permissions;
36 using System.Security.Claims;
38 namespace System.Security.Principal {
42 public class WindowsIdentity :
43 System.Security.Claims.ClaimsIdentity,
44 IIdentity, IDeserializationCallback, ISerializable, IDisposable {
45 private IntPtr _token;
47 private WindowsAccountType _account;
48 private bool _authenticated;
50 private SerializationInfo _info;
52 static private IntPtr invalidWindows = IntPtr.Zero;
55 public new const string DefaultIssuer = "AD AUTHORITY";
57 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
58 public WindowsIdentity (IntPtr userToken)
59 : this (userToken, null, WindowsAccountType.Normal, false)
63 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
64 public WindowsIdentity (IntPtr userToken, string type)
65 : this (userToken, type, WindowsAccountType.Normal, false)
69 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
70 public WindowsIdentity (IntPtr userToken, string type, WindowsAccountType acctType)
71 : this (userToken, type, acctType, false)
75 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
76 public WindowsIdentity (IntPtr userToken, string type, WindowsAccountType acctType, bool isAuthenticated)
80 _authenticated = isAuthenticated;
82 // last - as it can override some fields
86 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
87 public WindowsIdentity (string sUserPrincipalName)
88 : this (sUserPrincipalName, null)
92 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
93 public WindowsIdentity (string sUserPrincipalName, string type)
95 if (sUserPrincipalName == null)
96 throw new NullReferenceException ("sUserPrincipalName");
98 // TODO: Windows 2003 compatibility should be done in runtime
99 IntPtr token = GetUserToken (sUserPrincipalName);
100 if ((!Environment.IsUnix) && (token == IntPtr.Zero)) {
101 throw new ArgumentException ("only for Windows Server 2003 +");
104 _authenticated = true;
105 _account = WindowsAccountType.Normal;
107 // last - as it can override some fields
111 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
112 public WindowsIdentity (SerializationInfo info, StreamingContext context)
117 internal WindowsIdentity (ClaimsIdentity claimsIdentity, IntPtr userToken)
118 : base (claimsIdentity)
120 if (userToken != IntPtr.Zero && userToken.ToInt64() > 0)
122 SetToken (userToken);
127 public void Dispose ()
129 _token = IntPtr.Zero;
133 protected virtual void Dispose (bool disposing)
135 _token = IntPtr.Zero;
139 public static WindowsIdentity GetAnonymous ()
141 WindowsIdentity id = null;
142 if (Environment.IsUnix) {
143 id = new WindowsIdentity ("nobody");
145 id._account = WindowsAccountType.Anonymous;
146 id._authenticated = false;
147 id._type = String.Empty;
150 id = new WindowsIdentity (IntPtr.Zero, String.Empty, WindowsAccountType.Anonymous, false);
151 // special case (don't try to resolve the name)
152 id._name = String.Empty;
157 public static WindowsIdentity GetCurrent ()
159 return new WindowsIdentity (GetCurrentToken (), null, WindowsAccountType.Normal, true);
161 [MonoTODO ("need icall changes")]
162 public static WindowsIdentity GetCurrent (bool ifImpersonating)
164 throw new NotImplementedException ();
167 [MonoTODO ("need icall changes")]
168 public static WindowsIdentity GetCurrent (TokenAccessLevels desiredAccess)
170 throw new NotImplementedException ();
174 public virtual WindowsImpersonationContext Impersonate ()
176 return new WindowsImpersonationContext (_token);
179 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
180 public static WindowsImpersonationContext Impersonate (IntPtr userToken)
182 return new WindowsImpersonationContext (userToken);
187 public string AuthenticationType {
188 get { return _type; }
191 public virtual bool IsAnonymous
193 get { return (_account == WindowsAccountType.Anonymous); }
197 public bool IsAuthenticated
199 get { return _authenticated; }
202 public virtual bool IsGuest
204 get { return (_account == WindowsAccountType.Guest); }
207 public virtual bool IsSystem
209 get { return (_account == WindowsAccountType.System); }
217 // revolve name (runtime)
218 _name = GetTokenName (_token);
224 public virtual IntPtr Token
226 get { return _token; }
228 [MonoTODO ("not implemented")]
229 public IdentityReferenceCollection Groups {
230 get { throw new NotImplementedException (); }
233 [MonoTODO ("not implemented")]
235 public TokenImpersonationLevel ImpersonationLevel {
236 get { throw new NotImplementedException (); }
239 [MonoTODO ("not implemented")]
241 public SecurityIdentifier Owner {
242 get { throw new NotImplementedException (); }
245 [MonoTODO ("not implemented")]
247 public SecurityIdentifier User {
248 get { throw new NotImplementedException (); }
250 void IDeserializationCallback.OnDeserialization (object sender)
252 _token = (IntPtr) _info.GetValue ("m_userToken", typeof (IntPtr));
253 // can't trust this alone - we must validate the token
254 _name = _info.GetString ("m_name");
256 // validate token by comparing names
257 string name = GetTokenName (_token);
259 throw new SerializationException ("Token-Name mismatch.");
262 // validate token by getting name
263 _name = GetTokenName (_token);
265 throw new SerializationException ("Token doesn't match a user.");
267 _type = _info.GetString ("m_type");
268 _account = (WindowsAccountType) _info.GetValue ("m_acctType", typeof (WindowsAccountType));
269 _authenticated = _info.GetBoolean ("m_isAuthenticated");
272 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
274 info.AddValue ("m_userToken", _token);
275 // can be null when not resolved
276 info.AddValue ("m_name", _name);
277 info.AddValue ("m_type", _type);
278 info.AddValue ("m_acctType", _account);
279 info.AddValue ("m_isAuthenticated", _authenticated);
282 internal ClaimsIdentity CloneAsBase ()
287 internal IntPtr GetTokenInternal ()
292 private void SetToken (IntPtr token)
294 if (Environment.IsUnix) {
300 // override user choice in this specific case
301 if (_token == IntPtr.Zero)
302 _account = WindowsAccountType.System;
305 if ((token == invalidWindows) && (_account != WindowsAccountType.Anonymous))
306 throw new ArgumentException ("Invalid token");
315 // see mono/mono/metadata/security.c for implementation
317 // Many people use reflection to get a user's roles - so many
318 // that's it's hard to say it's an "undocumented" feature -
319 // so we also implement it in Mono :-/
320 // http://www.dotnet247.com/247reference/msgs/39/195403.aspx
321 [MethodImplAttribute (MethodImplOptions.InternalCall)]
322 internal extern static string[] _GetRoles (IntPtr token);
324 [MethodImplAttribute (MethodImplOptions.InternalCall)]
325 internal extern static IntPtr GetCurrentToken ();
327 [MethodImplAttribute (MethodImplOptions.InternalCall)]
328 private extern static string GetTokenName (IntPtr token);
330 [MethodImplAttribute (MethodImplOptions.InternalCall)]
331 private extern static IntPtr GetUserToken (string username);