2018-12-14 17:10:31 -05:00
|
|
|
|
#if (NET20 || NET35 || NET40 || NET45)
|
2017-11-27 18:50:18 -05:00
|
|
|
|
using System;
|
2017-11-27 13:11:20 -05:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.DirectoryServices.ActiveDirectory;
|
2019-08-17 16:42:49 -04:00
|
|
|
|
using Vanara.Extensions.Reflection;
|
2017-11-27 13:11:20 -05:00
|
|
|
|
using static Vanara.PInvoke.NTDSApi;
|
|
|
|
|
|
|
|
|
|
namespace Vanara.Extensions
|
|
|
|
|
{
|
2020-03-01 20:59:39 -05:00
|
|
|
|
/// <summary>Extensions for AD objects (e.g. DomainController).</summary>
|
2017-11-27 13:11:20 -05:00
|
|
|
|
public static class ActiveDirectoryExtension
|
|
|
|
|
{
|
2020-03-01 20:59:39 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Converts an array of directory service object names from one format to another. Name conversion enables client applications to
|
|
|
|
|
/// map between the multiple names used to identify various directory service objects. For example, user objects can be identified
|
|
|
|
|
/// by SAM account names (Domain\UserName), user principal name (UserName@Domain.com), or distinguished name.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dc">The <see cref="DomainController"/> instance to query.</param>
|
|
|
|
|
/// <param name="names">The names to convert.</param>
|
|
|
|
|
/// <param name="formatDesired">
|
|
|
|
|
/// Contains one of the DS_NAME_FORMAT values that identifies the format of the output names. The DS_SID_OR_SID_HISTORY_NAME value
|
|
|
|
|
/// is not supported.
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="formatOffered">Contains one of the DS_NAME_FORMAT values that identifies the format of the input names.</param>
|
|
|
|
|
/// <param name="flags">Contains one or more of the DS_NAME_FLAGS values used to determine how the name syntax will be cracked.</param>
|
|
|
|
|
/// <returns>The converted names.</returns>
|
2017-11-27 13:11:20 -05:00
|
|
|
|
public static string[] CrackNames(this DomainController dc, string[] names,
|
|
|
|
|
DS_NAME_FORMAT formatDesired = DS_NAME_FORMAT.DS_USER_PRINCIPAL_NAME,
|
|
|
|
|
DS_NAME_FORMAT formatOffered = DS_NAME_FORMAT.DS_NT4_ACCOUNT_NAME,
|
|
|
|
|
DS_NAME_FLAGS flags = DS_NAME_FLAGS.DS_NAME_NO_FLAGS)
|
|
|
|
|
{
|
|
|
|
|
lock (dc)
|
|
|
|
|
using (var ds = dc.GetHandle())
|
|
|
|
|
return DsCrackNames(ds, names, formatDesired, formatOffered, flags).Select(r => r.pName).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-01 20:59:39 -05:00
|
|
|
|
/// <summary>Uses reflection to get the hiddent handle of a <see cref="DomainController"/>.</summary>
|
|
|
|
|
/// <param name="dc">The <see cref="DomainController"/> instance.</param>
|
|
|
|
|
/// <returns>A <see cref="SafeDsHandle"/> for the domain controller in <paramref name="dc"/>.</returns>
|
|
|
|
|
/// <exception cref="InvalidOperationException"></exception>
|
2017-11-27 13:11:20 -05:00
|
|
|
|
public static SafeDsHandle GetHandle(this DomainController dc)
|
|
|
|
|
{
|
|
|
|
|
var hDc = dc.GetPropertyValue("Handle", IntPtr.Zero);
|
|
|
|
|
if (hDc == IntPtr.Zero) throw new InvalidOperationException();
|
|
|
|
|
return new SafeDsHandle(hDc);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-27 18:50:18 -05:00
|
|
|
|
#endif
|