2003-12-29 Sebastien Pouliot <spouliot@videotron.ca>
[mono.git] / mcs / class / corlib / System.Security.Principal / WindowsIdentity.cs
1 //
2 // System.Security.Principal.WindowsIdentity
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //      Sebastien Pouliot (spouliot@motus.com)
7 //
8 // (C) 2002 Ximian, Inc (http://www.ximian.com)
9 // Portions (C) 2003 Motus Technologies Inc. (http://www.motus.com)
10 //
11
12 using System;
13 using System.Runtime.Serialization;
14
15 namespace System.Security.Principal {
16
17         [Serializable]
18 #if NET_1_0
19         public class WindowsIdentity : IIdentity, IDeserializationCallback {
20 #else
21         public class WindowsIdentity : IIdentity, IDeserializationCallback, ISerializable {
22 #endif
23                 private IntPtr _token;
24                 private string _type;
25                 private WindowsAccountType _account;
26                 private bool _authenticated;
27                 private string _name;
28
29                 public WindowsIdentity (IntPtr userToken) 
30                         : this (userToken, "NTLM", WindowsAccountType.Normal, false) {}
31
32                 public WindowsIdentity (IntPtr userToken, string type) 
33                         : this (userToken, type, WindowsAccountType.Normal, false) {}
34
35                 public WindowsIdentity (IntPtr userToken, string type, WindowsAccountType acctType)
36                         : this (userToken, type, acctType, false) {}
37
38                 public WindowsIdentity (IntPtr userToken, string type, WindowsAccountType acctType, bool isAuthenticated)
39                 {
40                         if (userToken == IntPtr.Zero)
41                                 throw new ArgumentException ("Invalid token");
42
43                         _token = userToken;
44                         _type = type;
45                         _account = acctType;
46                         _authenticated = isAuthenticated;
47                         _name = null;
48                 }
49 #if !NET_1_0
50                 public WindowsIdentity (string sUserPrincipalName) 
51                         : this (sUserPrincipalName, null) {}
52
53                 [MonoTODO]
54                 public WindowsIdentity (string sUserPrincipalName, string type)
55                 {
56                         if (sUserPrincipalName == null)
57                                 throw new NullReferenceException ("sUserPrincipalName");
58
59                         throw new ArgumentException ("only for Windows Server 2003 +");
60                 }
61
62                 [MonoTODO]
63                 public WindowsIdentity (SerializationInfo info, StreamingContext context) {}
64 #endif
65                 [MonoTODO]
66                 ~WindowsIdentity ()
67                 {
68                         _token = IntPtr.Zero;
69                 }
70
71                 // static methods
72
73                 public static WindowsIdentity GetAnonymous ()
74                 {
75                         WindowsIdentity id = new WindowsIdentity ((IntPtr)1, String.Empty, WindowsAccountType.Anonymous, false);
76                         // special case
77                         id._token = IntPtr.Zero;
78                         id._name = String.Empty;
79                         return id;
80                 }
81
82                 [MonoTODO]
83                 public static WindowsIdentity GetCurrent ()
84                 {
85                         throw new NotImplementedException ();
86                 }
87
88                 // methods
89
90                 public virtual WindowsImpersonationContext Impersonate ()
91                 {
92                         return new WindowsImpersonationContext (_token);
93                 }
94
95                 public static WindowsImpersonationContext Impersonate (IntPtr userToken)
96                 {
97                         return new WindowsImpersonationContext (userToken);
98                 }
99
100                 // properties
101
102                 public virtual string AuthenticationType
103                 {
104                         get { return _type; }
105                 }
106
107                 public virtual bool IsAnonymous
108                 {
109                         get { return (_account == WindowsAccountType.Anonymous); }
110                 }
111
112                 public virtual bool IsAuthenticated
113                 {
114                         get { return _authenticated; }
115                 }
116
117                 public virtual bool IsGuest
118                 {
119                         get { return (_account == WindowsAccountType.Guest); }
120                 }
121
122                 public virtual bool IsSystem
123                 {
124                         get { return (_account == WindowsAccountType.System); }
125                 }
126
127                 [MonoTODO ("resolve missing")]
128                 public virtual string Name
129                 {
130                         get {
131                                 if (_name == null) {
132                                         // TODO: resolve name from token
133                                         throw new NotImplementedException ();
134                                 }
135                                 return _name; 
136                         }
137                 }
138
139                 public virtual IntPtr Token
140                 {
141                         get { return _token; }
142                 }
143
144                 [MonoTODO]
145                 void IDeserializationCallback.OnDeserialization (object sender)
146                 {
147                         throw new NotImplementedException ();
148                 }
149 #if !NET_1_0
150                 [MonoTODO]
151                 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context) 
152                 {
153                         throw new NotImplementedException ();
154                 }
155 #endif
156         }
157 }
158