• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2181
  • Last Modified:

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
0
Jaya1983
Asked:
Jaya1983
1 Solution
 
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
 
Jaya1983Author Commented:
Good
0

Featured Post

Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now