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

Hi,

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

Please let me know if anybody having this code.

Thanks,

Naveen
Jaya1983Asked:
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.

pubeheedCommented:
does this have to be specifally in c# or could you use powershell and then import it from a csv?
0
mvcampCommented:
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)
        {
            try
            {
                //--- Get the naming contexts ----
                DirectoryEntry myRootDSE = new DirectoryEntry(@"LDAP://RootDSE");
                myRootDSE.RefreshCache();
                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");

                Console.WriteLine("Suffixes");
                foreach (string str in suffixes)
                {
                    Console.WriteLine(str);
                }
                Console.ReadLine();

                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));
                Console.ReadLine();
            }

        }

    }
}

Open in new window

0

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:
Good
0
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.