2 using Microsoft.Win32.SafeHandles;
12 private readonly
string _Name;
14 internal const int MaximumAccountNameLength = 256;
16 internal const int MaximumDomainNameLength = 255;
31 public NTAccount(
string domainName,
string accountName)
33 if (accountName ==
null)
37 if (accountName.Length == 0)
41 if (accountName.Length > 256)
45 if (domainName !=
null && domainName.Length > 255)
49 if (domainName ==
null || domainName.Length == 0)
55 _Name = domainName +
"\\" + accountName;
76 if (name.Length > 512)
109 [SecuritySafeCritical]
113 if (targetType ==
null)
124 identityReferenceCollection.
Add(
this);
126 return identityReferenceCollection2[0];
142 if (nTAccount ==
null)
146 return this == nTAccount;
166 bool someFailed =
false;
168 if (forceSuccess && someFailed)
173 if (item.GetType() != targetType)
175 identityReferenceCollection2.
Add(item);
178 throw new IdentityNotMappedException(
Environment.GetResourceString(
"IdentityReference_IdentityNotMapped"), identityReferenceCollection2);
180 return identityReferenceCollection;
184 internal static IdentityReferenceCollection
Translate(IdentityReferenceCollection sourceAccounts, Type targetType, out
bool someFailed)
186 if (sourceAccounts ==
null)
188 throw new ArgumentNullException(
"sourceAccounts");
190 if (targetType == typeof(SecurityIdentifier))
192 return TranslateToSids(sourceAccounts, out someFailed);
194 throw new ArgumentException(Environment.GetResourceString(
"IdentityReference_MustBeIdentityReference"),
"targetType");
204 if ((
object)left ==
null && (
object)right ==
null)
208 if ((
object)left ==
null || (
object)right ==
null)
222 return !(left == right);
228 if (sourceAccounts ==
null)
232 if (sourceAccounts.
Count == 0)
236 SafeLsaPolicyHandle safeLsaPolicyHandle = SafeLsaPolicyHandle.InvalidHandle;
237 SafeLsaMemoryHandle referencedDomains = SafeLsaMemoryHandle.InvalidHandle;
238 SafeLsaMemoryHandle sids = SafeLsaMemoryHandle.InvalidHandle;
241 Win32Native.UNICODE_STRING[] array =
new Win32Native.UNICODE_STRING[sourceAccounts.
Count];
243 foreach (IdentityReference sourceAccount
in sourceAccounts)
246 if (nTAccount ==
null)
250 array[num].Buffer = nTAccount.ToString();
251 if (array[num].Buffer.Length * 2 + 2 > 65535)
253 throw new SystemException();
255 array[num].Length = (ushort)(array[num].Buffer.Length * 2);
256 array[num].MaximumLength = (ushort)(array[num].Length + 2);
259 safeLsaPolicyHandle = Win32.LsaOpenPolicy(
null, PolicyRights.POLICY_LOOKUP_NAMES);
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);
266 throw new OutOfMemoryException();
268 throw new UnauthorizedAccessException();
275 int errorCode = Win32Native.LsaNtStatusToWinError((
int)num2);
276 throw new SystemException(Win32Native.GetMessage(errorCode));
281 IdentityReferenceCollection identityReferenceCollection =
new IdentityReferenceCollection(sourceAccounts.Count);
282 if (num2 == 0 || num2 == 263)
284 if (Win32.LsaLookupNames2Supported)
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++)
292 Win32Native.LSA_TRANSLATED_SID2 lSA_TRANSLATED_SID = array2[i];
293 switch (lSA_TRANSLATED_SID.Use)
300 identityReferenceCollection.Add(
new SecurityIdentifier(lSA_TRANSLATED_SID.Sid, noDemand:
true));
304 identityReferenceCollection.Add(sourceAccounts[i]);
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++)
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);
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++)
324 Win32Native.LSA_TRANSLATED_SID lSA_TRANSLATED_SID2 = array4[k];
325 switch (lSA_TRANSLATED_SID2.Use)
332 identityReferenceCollection.Add(
new SecurityIdentifier(array3[lSA_TRANSLATED_SID2.DomainIndex], lSA_TRANSLATED_SID2.Rid));
336 identityReferenceCollection.Add(sourceAccounts[k]);
344 for (
int l = 0; l < sourceAccounts.Count; l++)
346 identityReferenceCollection.Add(sourceAccounts[l]);
349 return identityReferenceCollection;
353 safeLsaPolicyHandle.Dispose();
354 referencedDomains.Dispose();
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....
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...
override bool Equals(object o)
Returns a value that indicates whether this T:System.Security.Principal.NTAccount object is equal to ...
void Add(IdentityReference identity)
Adds an T:System.Security.Principal.IdentityReference object to the T:System.Security....
static int SizeOf(object structure)
Returns the unmanaged size of an object in bytes.
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....
Represents a user or group account.
SecurityAction
Specifies the security actions that can be performed using declarative security.
Provides information about, and means to manipulate, the current environment and platform....
Represents type declarations: class types, interface types, array types, value types,...
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
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....
static void PtrToStructure(IntPtr ptr, object structure)
Marshals data from an unmanaged block of memory to a managed object.
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 ...
override IdentityReference Translate(Type targetType)
Translates the account name represented by the T:System.Security.Principal.NTAccount object into anot...
static bool operator==(NTAccount left, NTAccount right)
Compares two T:System.Security.Principal.NTAccount objects to determine whether they are equal....
NTAccount(string name)
Initializes a new instance of the T:System.Security.Principal.NTAccount class by using the specified ...
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.