From 4b866c94864bd82c475ff58ffd138f6605118470 Mon Sep 17 00:00:00 2001 From: Gary Sharp Date: Thu, 15 Sep 2016 19:45:14 +1000 Subject: [PATCH] Escape AD Distinguished Names that contain "/" for EFC --- .../ActiveDirectory/ADDomainController.cs | 2 +- .../Interop/ActiveDirectory/ADHelpers.cs | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/Disco.Services/Interop/ActiveDirectory/ADDomainController.cs b/Disco.Services/Interop/ActiveDirectory/ADDomainController.cs index c5dddf9f..d505bca3 100644 --- a/Disco.Services/Interop/ActiveDirectory/ADDomainController.cs +++ b/Disco.Services/Interop/ActiveDirectory/ADDomainController.cs @@ -67,7 +67,7 @@ namespace Disco.Services.Interop.ActiveDirectory if (!DistinguishedName.EndsWith(this.Domain.DistinguishedName, StringComparison.OrdinalIgnoreCase)) throw new ArgumentException(string.Format("The Distinguished Name ({0}) isn't a member of this domain [{1}]", DistinguishedName, this.Domain.Name), "DistinguishedName"); - var entry = new DirectoryEntry(string.Format(LdapPathTemplate, this.Name, DistinguishedName)); + var entry = new DirectoryEntry(string.Format(LdapPathTemplate, this.Name, ADHelpers.EscapeDistinguishedName(DistinguishedName))); if (LoadProperties != null) entry.RefreshCache(LoadProperties); diff --git a/Disco.Services/Interop/ActiveDirectory/ADHelpers.cs b/Disco.Services/Interop/ActiveDirectory/ADHelpers.cs index 07b2eed5..1e002273 100644 --- a/Disco.Services/Interop/ActiveDirectory/ADHelpers.cs +++ b/Disco.Services/Interop/ActiveDirectory/ADHelpers.cs @@ -1,4 +1,5 @@ -using System.Security.Principal; +using System; +using System.Security.Principal; using System.Text; namespace Disco.Services.Interop.ActiveDirectory @@ -47,11 +48,23 @@ namespace Disco.Services.Interop.ActiveDirectory return query.Replace("*", "\\2a").Replace("(", "\\28").Replace(")", "\\29").Replace("\\", "\\5c").Replace("NUL", "\\00").Replace("/", "\\2f"); } - internal static string ToLdapQueryFormat(this System.Guid g) + internal static string EscapeDistinguishedName(string DistinguishedName) + { + if (DistinguishedName.Contains("/")) + { + return DistinguishedName.Replace("/", @"\/"); + } + else + { + return DistinguishedName; + } + } + + internal static string ToLdapQueryFormat(this Guid g) { checked { - System.Text.StringBuilder sb = new System.Text.StringBuilder(); + StringBuilder sb = new StringBuilder(); byte[] array = g.ToByteArray(); for (int i = 0; i < array.Length; i++) {