Add and Remove an Active Directory Domain Suffix using C#

William Yeack

While working on some of our upcoming enterprise features over at Member.buzz, I ran into an issue that wasn't very well documented.

Using the excellent System.DirectoryServices namespace we are able to manage the domain suffixes added to Active Directory.

First, this is how you can retrieve the current suffixes:

⁠List Result = new List();
DirectoryEntry Partition = new DirectoryEntry(@"LDAP://CN=Partitions," + (new DirectoryEntry(@"LDAP://RootDSE").Properties["configurationNamingContext"].Value.ToString()));
var SuffixCollection = Partition.Properties["uPNSuffixes"];foreach (var Suffix in SuffixCollection)
{
Result.Add(Suffix.ToString());
}

Next, we add a new suffix:

⁠var SuffixToAdd = "domain.com";
DirectoryEntry Partition = new DirectoryEntry(@"LDAP://CN=Partitions," + (new DirectoryEntry(@"LDAP://RootDSE").Properties["configurationNamingContext"].Value.ToString()));Partition.Properties["uPNSuffixes"].Add(SuffixToAdd);
Partition.CommitChanges();

Finally, removing a suffix is a little bit tricky.  While there is a Remove method in the Properties object, removing a value doesn't seem to persist correctly.  Instead, we create a new array, copy the appopriate values and override the existing value.

var SuffixToRemove = "domain.com";
DirectoryEntry Partition = new DirectoryEntry(@"LDAP://CN=Partitions," + (new DirectoryEntry(@"LDAP://RootDSE").Properties["configurationNamingContext"].Value.ToString()));
var Suffixes = Partition.Properties["uPNSuffixes"];
var SuffixList = new List();
var SuffixCollection = Partition.Properties["uPNSuffixes"];
foreach (var Suffix in SuffixCollection)
{
if (Suffix.ToString() == SuffixToRemove) { continue; }
SuffixList.Add(Suffix.ToString());
}
Partition.Properties["uPNSuffixes"].Value = SuffixList.ToArray();Partition.CommitChanges();

And there you have it!

Comments

To add a comment, please login or register.

Related

Using a Lenovo P51 Laptop with an Airplane Power Supply
The Lenovo P51 Laptop comes with a huge 170 watt Power Supply. However, airplane power supplies provide a maximum of somewhere between 75-100 watts. If you plugin a power supply requiring more watts, the circuit breaker will short out and the power will stop flowing.
Setup Point-to-Site VPN with Ubiquiti EdgeRouter
Learn how to setup a VPN with your Ubiquiti EdgeRouter.
Turning Atlassian JIRA into a CRM
Here at Member.buzz, we use Atlassian JIRA to track our features, bugs, and incoming requests from users through our Support Site. So when it came to choosing a CRM, we wanted to find one that integrated nicely with the rest of our infrastructure.Our first thought was to try out some of the existing JIRA CRM plugins. Here are the ones we tried out:CRM for JIRAAtlas CRMKanoah CRMAlthough there were definitely some interesting features among these options, there was nothing substantial enough to make us want to choose a specific one. We wanted something simple, yet well-integrated into what we already had
C-Level Security: When your team uses military analogies, are they using the wrong narrative?
For years, I have bristled when people would use medieval military descriptions in an attempt to convey concepts within the Network Security business. Bastions, Firewalls, Moats, Drawbridges, Countermeasures; all of these descriptions give way to a more accurate and detailed explanation of what was really taking place.