mscorlib(4.0.0.0) API with additions
NTAccount.cs
1 using Microsoft.Win32;
2 using Microsoft.Win32.SafeHandles;
5 
7 {
9  [ComVisible(false)]
10  public sealed class NTAccount : IdentityReference
11  {
12  private readonly string _Name;
13 
14  internal const int MaximumAccountNameLength = 256;
15 
16  internal const int MaximumDomainNameLength = 255;
17 
20  public override string Value => ToString();
21 
31  public NTAccount(string domainName, string accountName)
32  {
33  if (accountName == null)
34  {
35  throw new ArgumentNullException("accountName");
36  }
37  if (accountName.Length == 0)
38  {
39  throw new ArgumentException(Environment.GetResourceString("Argument_StringZeroLength"), "accountName");
40  }
41  if (accountName.Length > 256)
42  {
43  throw new ArgumentException(Environment.GetResourceString("IdentityReference_AccountNameTooLong"), "accountName");
44  }
45  if (domainName != null && domainName.Length > 255)
46  {
47  throw new ArgumentException(Environment.GetResourceString("IdentityReference_DomainNameTooLong"), "domainName");
48  }
49  if (domainName == null || domainName.Length == 0)
50  {
51  _Name = accountName;
52  }
53  else
54  {
55  _Name = domainName + "\\" + accountName;
56  }
57  }
58 
66  public NTAccount(string name)
67  {
68  if (name == null)
69  {
70  throw new ArgumentNullException("name");
71  }
72  if (name.Length == 0)
73  {
74  throw new ArgumentException(Environment.GetResourceString("Argument_StringZeroLength"), "name");
75  }
76  if (name.Length > 512)
77  {
78  throw new ArgumentException(Environment.GetResourceString("IdentityReference_AccountNameTooLong"), "name");
79  }
80  _Name = name;
81  }
82 
87  public override bool IsValidTargetType(Type targetType)
88  {
89  if (targetType == typeof(SecurityIdentifier))
90  {
91  return true;
92  }
93  if (targetType == typeof(NTAccount))
94  {
95  return true;
96  }
97  return false;
98  }
99 
109  [SecuritySafeCritical]
110  [SecurityPermission(SecurityAction.Demand, ControlPrincipal = true)]
111  public override IdentityReference Translate(Type targetType)
112  {
113  if (targetType == null)
114  {
115  throw new ArgumentNullException("targetType");
116  }
117  if (targetType == typeof(NTAccount))
118  {
119  return this;
120  }
121  if (targetType == typeof(SecurityIdentifier))
122  {
123  IdentityReferenceCollection identityReferenceCollection = new IdentityReferenceCollection(1);
124  identityReferenceCollection.Add(this);
125  IdentityReferenceCollection identityReferenceCollection2 = Translate(identityReferenceCollection, targetType, forceSuccess: true);
126  return identityReferenceCollection2[0];
127  }
128  throw new ArgumentException(Environment.GetResourceString("IdentityReference_MustBeIdentityReference"), "targetType");
129  }
130 
135  public override bool Equals(object o)
136  {
137  if (o == null)
138  {
139  return false;
140  }
141  NTAccount nTAccount = o as NTAccount;
142  if (nTAccount == null)
143  {
144  return false;
145  }
146  return this == nTAccount;
147  }
148 
151  public override int GetHashCode()
152  {
154  }
155 
158  public override string ToString()
159  {
160  return _Name;
161  }
162 
163  [SecurityCritical]
164  internal static IdentityReferenceCollection Translate(IdentityReferenceCollection sourceAccounts, Type targetType, bool forceSuccess)
165  {
166  bool someFailed = false;
167  IdentityReferenceCollection identityReferenceCollection = Translate(sourceAccounts, targetType, out someFailed);
168  if (forceSuccess && someFailed)
169  {
170  IdentityReferenceCollection identityReferenceCollection2 = new IdentityReferenceCollection();
171  foreach (IdentityReference item in identityReferenceCollection)
172  {
173  if (item.GetType() != targetType)
174  {
175  identityReferenceCollection2.Add(item);
176  }
177  }
178  throw new IdentityNotMappedException(Environment.GetResourceString("IdentityReference_IdentityNotMapped"), identityReferenceCollection2);
179  }
180  return identityReferenceCollection;
181  }
182 
183  [SecurityCritical]
184  internal static IdentityReferenceCollection Translate(IdentityReferenceCollection sourceAccounts, Type targetType, out bool someFailed)
185  {
186  if (sourceAccounts == null)
187  {
188  throw new ArgumentNullException("sourceAccounts");
189  }
190  if (targetType == typeof(SecurityIdentifier))
191  {
192  return TranslateToSids(sourceAccounts, out someFailed);
193  }
194  throw new ArgumentException(Environment.GetResourceString("IdentityReference_MustBeIdentityReference"), "targetType");
195  }
196 
202  public static bool operator ==(NTAccount left, NTAccount right)
203  {
204  if ((object)left == null && (object)right == null)
205  {
206  return true;
207  }
208  if ((object)left == null || (object)right == null)
209  {
210  return false;
211  }
212  return left.ToString().Equals(right.ToString(), StringComparison.OrdinalIgnoreCase);
213  }
214 
220  public static bool operator !=(NTAccount left, NTAccount right)
221  {
222  return !(left == right);
223  }
224 
225  [SecurityCritical]
226  private static IdentityReferenceCollection TranslateToSids(IdentityReferenceCollection sourceAccounts, out bool someFailed)
227  {
228  if (sourceAccounts == null)
229  {
230  throw new ArgumentNullException("sourceAccounts");
231  }
232  if (sourceAccounts.Count == 0)
233  {
234  throw new ArgumentException(Environment.GetResourceString("Arg_EmptyCollection"), "sourceAccounts");
235  }
236  SafeLsaPolicyHandle safeLsaPolicyHandle = SafeLsaPolicyHandle.InvalidHandle;
237  SafeLsaMemoryHandle referencedDomains = SafeLsaMemoryHandle.InvalidHandle;
238  SafeLsaMemoryHandle sids = SafeLsaMemoryHandle.InvalidHandle;
239  try
240  {
241  Win32Native.UNICODE_STRING[] array = new Win32Native.UNICODE_STRING[sourceAccounts.Count];
242  int num = 0;
243  foreach (IdentityReference sourceAccount in sourceAccounts)
244  {
245  NTAccount nTAccount = sourceAccount as NTAccount;
246  if (nTAccount == null)
247  {
248  throw new ArgumentException(Environment.GetResourceString("Argument_ImproperType"), "sourceAccounts");
249  }
250  array[num].Buffer = nTAccount.ToString();
251  if (array[num].Buffer.Length * 2 + 2 > 65535)
252  {
253  throw new SystemException();
254  }
255  array[num].Length = (ushort)(array[num].Buffer.Length * 2);
256  array[num].MaximumLength = (ushort)(array[num].Length + 2);
257  num++;
258  }
259  safeLsaPolicyHandle = Win32.LsaOpenPolicy(null, PolicyRights.POLICY_LOOKUP_NAMES);
260  someFailed = false;
261  uint num2 = (!Win32.LsaLookupNames2Supported) ? Win32Native.LsaLookupNames(safeLsaPolicyHandle, sourceAccounts.Count, array, ref referencedDomains, ref sids) : Win32Native.LsaLookupNames2(safeLsaPolicyHandle, 0, sourceAccounts.Count, array, ref referencedDomains, ref sids);
262  switch (num2)
263  {
264  case 3221225495u:
265  case 3221225626u:
266  throw new OutOfMemoryException();
267  case 3221225506u:
268  throw new UnauthorizedAccessException();
269  case 3221225587u:
270  case 263u:
271  someFailed = true;
272  break;
273  default:
274  {
275  int errorCode = Win32Native.LsaNtStatusToWinError((int)num2);
276  throw new SystemException(Win32Native.GetMessage(errorCode));
277  }
278  case 0u:
279  break;
280  }
281  IdentityReferenceCollection identityReferenceCollection = new IdentityReferenceCollection(sourceAccounts.Count);
282  if (num2 == 0 || num2 == 263)
283  {
284  if (Win32.LsaLookupNames2Supported)
285  {
286  sids.Initialize((uint)sourceAccounts.Count, (uint)Marshal.SizeOf(typeof(Win32Native.LSA_TRANSLATED_SID2)));
287  Win32.InitializeReferencedDomainsPointer(referencedDomains);
288  Win32Native.LSA_TRANSLATED_SID2[] array2 = new Win32Native.LSA_TRANSLATED_SID2[sourceAccounts.Count];
289  sids.ReadArray(0uL, array2, 0, array2.Length);
290  for (int i = 0; i < sourceAccounts.Count; i++)
291  {
292  Win32Native.LSA_TRANSLATED_SID2 lSA_TRANSLATED_SID = array2[i];
293  switch (lSA_TRANSLATED_SID.Use)
294  {
295  case 1:
296  case 2:
297  case 4:
298  case 5:
299  case 9:
300  identityReferenceCollection.Add(new SecurityIdentifier(lSA_TRANSLATED_SID.Sid, noDemand: true));
301  break;
302  default:
303  someFailed = true;
304  identityReferenceCollection.Add(sourceAccounts[i]);
305  break;
306  }
307  }
308  }
309  else
310  {
311  sids.Initialize((uint)sourceAccounts.Count, (uint)Marshal.SizeOf(typeof(Win32Native.LSA_TRANSLATED_SID)));
312  Win32.InitializeReferencedDomainsPointer(referencedDomains);
313  Win32Native.LSA_REFERENCED_DOMAIN_LIST lSA_REFERENCED_DOMAIN_LIST = referencedDomains.Read<Win32Native.LSA_REFERENCED_DOMAIN_LIST>(0uL);
314  SecurityIdentifier[] array3 = new SecurityIdentifier[lSA_REFERENCED_DOMAIN_LIST.Entries];
315  for (int j = 0; j < lSA_REFERENCED_DOMAIN_LIST.Entries; j++)
316  {
317  Win32Native.LSA_TRUST_INFORMATION lSA_TRUST_INFORMATION = (Win32Native.LSA_TRUST_INFORMATION)Marshal.PtrToStructure(new IntPtr((long)lSA_REFERENCED_DOMAIN_LIST.Domains + j * Marshal.SizeOf(typeof(Win32Native.LSA_TRUST_INFORMATION))), typeof(Win32Native.LSA_TRUST_INFORMATION));
318  array3[j] = new SecurityIdentifier(lSA_TRUST_INFORMATION.Sid, noDemand: true);
319  }
320  Win32Native.LSA_TRANSLATED_SID[] array4 = new Win32Native.LSA_TRANSLATED_SID[sourceAccounts.Count];
321  sids.ReadArray(0uL, array4, 0, array4.Length);
322  for (int k = 0; k < sourceAccounts.Count; k++)
323  {
324  Win32Native.LSA_TRANSLATED_SID lSA_TRANSLATED_SID2 = array4[k];
325  switch (lSA_TRANSLATED_SID2.Use)
326  {
327  case 1:
328  case 2:
329  case 4:
330  case 5:
331  case 9:
332  identityReferenceCollection.Add(new SecurityIdentifier(array3[lSA_TRANSLATED_SID2.DomainIndex], lSA_TRANSLATED_SID2.Rid));
333  break;
334  default:
335  someFailed = true;
336  identityReferenceCollection.Add(sourceAccounts[k]);
337  break;
338  }
339  }
340  }
341  }
342  else
343  {
344  for (int l = 0; l < sourceAccounts.Count; l++)
345  {
346  identityReferenceCollection.Add(sourceAccounts[l]);
347  }
348  }
349  return identityReferenceCollection;
350  }
351  finally
352  {
353  safeLsaPolicyHandle.Dispose();
354  referencedDomains.Dispose();
355  sids.Dispose();
356  }
357  }
358  }
359 }
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Describes a set of security permissions applied to code. This class cannot be inherited.
int GetHashCode(object obj)
When overridden in a derived class, gets the hash code for the specified object.
Represents an identity and is the base class for the T:System.Security.Principal.NTAccount and T:Syst...
override int GetHashCode()
Serves as a hash function for the current T:System.Security.Principal.NTAccount object....
Definition: NTAccount.cs:151
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
override bool IsValidTargetType(Type targetType)
Returns a value that indicates whether the specified type is a valid translation type for the T:Syste...
Definition: NTAccount.cs:87
override bool Equals(object o)
Returns a value that indicates whether this T:System.Security.Principal.NTAccount object is equal to ...
Definition: NTAccount.cs:135
void Add(IdentityReference identity)
Adds an T:System.Security.Principal.IdentityReference object to the T:System.Security....
Definition: __Canon.cs:3
static int SizeOf(object structure)
Returns the unmanaged size of an object in bytes.
Definition: Marshal.cs:159
int Count
Gets the number of items in the T:System.Security.Principal.IdentityReferenceCollection collection.
override string ToString()
Returns the account name, in Domain\Account format, for the account represented by the T:System....
Definition: NTAccount.cs:158
Represents a user or group account.
Definition: NTAccount.cs:10
SecurityAction
Specifies the security actions that can be performed using declarative security.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
Definition: Marshal.cs:15
Represents a collection of T:System.Security.Principal.IdentityReference objects and provides a means...
static StringComparer InvariantCultureIgnoreCase
Gets a T:System.StringComparer object that performs a case-insensitive string comparison using the wo...
The exception that is thrown when one of the arguments provided to a method is not valid.
static bool operator !=(NTAccount left, NTAccount right)
Compares two T:System.Security.Principal.NTAccount objects to determine whether they are not equal....
Definition: NTAccount.cs:220
static void PtrToStructure(IntPtr ptr, object structure)
Marshals data from an unmanaged block of memory to a managed object.
Definition: Marshal.cs:1198
Represents a security identifier (SID) and provides marshaling and comparison operations for SIDs.
NTAccount(string domainName, string accountName)
Initializes a new instance of the T:System.Security.Principal.NTAccount class by using the specified ...
Definition: NTAccount.cs:31
override IdentityReference Translate(Type targetType)
Translates the account name represented by the T:System.Security.Principal.NTAccount object into anot...
Definition: NTAccount.cs:111
static bool operator==(NTAccount left, NTAccount right)
Compares two T:System.Security.Principal.NTAccount objects to determine whether they are equal....
Definition: NTAccount.cs:202
NTAccount(string name)
Initializes a new instance of the T:System.Security.Principal.NTAccount class by using the specified ...
Definition: NTAccount.cs:66
Represents a string comparison operation that uses specific case and culture-based or ordinal compari...
override string Value
Returns an uppercase string representation of this T:System.Security.Principal.NTAccount object.
Definition: NTAccount.cs:20