How to get list all UPN Suffixes from AD in C# code.


How to get  list all UPN Suffixes from AD in C# code.

Please let me know if anybody having this code.


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

does this have to be specifally in c# or could you use powershell and then import it from a csv?
Using the Directory Services in .NET you should be able to list all Directory Entries and Properties you are allowed to see.

For the uPNSuffixes you need to have Domain Administrator Privileges in order to load them up in the property cache.

You can use following C# code to retrieve the UPN Suffixes (Paste it in an empty C# Console project):
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;

namespace GetUPNFromAD
    class Program
        static void Main(string[] args)
                //--- Get the naming contexts ----
                DirectoryEntry myRootDSE = new DirectoryEntry(@"LDAP://RootDSE");
                string strNamingContext = myRootDSE.Properties["defaultNamingContext"].Value.ToString();
                string strConfigContext = myRootDSE.Properties["configurationNamingContext"].Value.ToString();

                //--- Get the current domain name --
                DirectoryEntry oDomain = new DirectoryEntry(@"LDAP://" + strNamingContext);
                string strDomainName = oDomain.Properties["name"].Value.ToString();

                DirectoryEntry oPartition = new DirectoryEntry(@"LDAP://CN=Partitions," + strConfigContext);

                //--- Get the DNS name of the domain
                oDomain.Invoke("GetInfoEx", new object[] { "canonicalName" }, 0);
                string strCanonical = oDomain.InvokeGet("canonicalName").ToString();
                strCanonical = strCanonical.Replace("/", "");   // strip the "/" at the end of the domainname

                Console.WriteLine("Domain: " + strCanonical);

                oPartition.Invoke("GetEx", new object[] { "uPNSuffixes" });
                string[] suffixes = (string[])oPartition.InvokeGet("uPNSuffixes");

                foreach (string str in suffixes)

                myRootDSE = null;
                oDomain = null;
                oPartition = null;
            catch (Exception ex)
                Console.WriteLine(string.Format("Exception occured: {0}\n Inner Exception: \n {1}", ex.Message, ex.InnerException));



Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jaya1983Author Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.