Ejemplo n.º 1
0
        /// <summary>
        /// Get a list domains in the current forest. Fetches the simple domain name for each domain
        /// </summary>
        /// <returns>A list of Domains sorted by their simple domain names</returns>
        public static SortedList <string, Domain> getCurrentDomains()
        {
            try
            {
                Forest                      forest     = Domain.GetCurrentDomain().Forest;
                DomainCollection            domains    = forest.Domains;
                SortedList <string, Domain> sortedList = new SortedList <string, Domain>();

                foreach (Domain d in domains)
                {
                    String distinguishedName = getTranslatableName(d.Name);

                    ActiveDs.NameTranslate tranlator = new ActiveDs.NameTranslate();
                    tranlator.Set((int)ActiveDs.ADS_NAME_TYPE_ENUM.ADS_NAME_TYPE_1779, distinguishedName);
                    String translation = tranlator.Get((int)ActiveDs.ADS_NAME_TYPE_ENUM.ADS_NAME_TYPE_NT4);
                    translation = translation.Replace(@"\", "");
                    sortedList.Add(translation, d);
                }

                return(sortedList);
            }
            catch (Exception)
            {
                return(null);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Convert from an Active Directory User Principal Name (UPN) to a down-level NT4 style name.
 /// </summary>
 public static string TranslateUpnToDownLevel(string userPrincipalName)
 {
     string downLevelName;
     ActiveDs.NameTranslate nt = new ActiveDs.NameTranslate();
     nt.Set(NameTypeUpn, userPrincipalName);
     downLevelName = nt.Get(NameTypeNt4);
     return downLevelName;
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Convert from a down-level NT4 style name to an Active Directory User Principal Name (UPN).
 /// </summary>
 public static string TranslateDownLevelToUpn(string downLevelNt4Name)
 {
     string userPrincipalName;
     ActiveDs.NameTranslate nt = new ActiveDs.NameTranslate();
     nt.Set(NameTypeNt4, downLevelNt4Name);
     userPrincipalName = nt.Get(NameTypeUpn);
     return userPrincipalName;
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Convert from an Active Directory User Principal Name (UPN) to a down-level NT4 style name.
        /// </summary>
        public static string TranslateUpnToDownLevel(string userPrincipalName)
        {
            string downLevelName;

            ActiveDs.NameTranslate nt = new ActiveDs.NameTranslate();
            nt.Set(NameTypeUpn, userPrincipalName);
            downLevelName = nt.Get(NameTypeNt4);
            return(downLevelName);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Convert from a down-level NT4 style name to an Active Directory User Principal Name (UPN).
        /// </summary>
        public static string TranslateDownLevelToUpn(string downLevelNt4Name)
        {
            string userPrincipalName;

            ActiveDs.NameTranslate nt = new ActiveDs.NameTranslate();
            nt.Set(NameTypeNt4, downLevelNt4Name);
            userPrincipalName = nt.Get(NameTypeUpn);
            return(userPrincipalName);
        }
Ejemplo n.º 6
0
        public static IEnumerable <string> Convert_ADName(Args_Convert_ADName args)
        {
            ADSNameType ADSInitType;
            string      InitName;

            // https://msdn.microsoft.com/en-us/library/aa772266%28v=vs.85%29.aspx
            if (args.Server.IsNotNullOrEmpty())
            {
                ADSInitType = ADSNameType.Canonical;
                InitName    = args.Server;
            }
            else if (args.Domain.IsNotNullOrEmpty())
            {
                ADSInitType = ADSNameType.DN;
                InitName    = args.Domain;
            }
            else if (args.Credential != null)
            {
                ADSInitType = ADSNameType.DN;
                InitName    = args.Credential.Domain;
            }
            else
            {
                // if no domain or server is specified, default to GC initialization
                ADSInitType = ADSNameType.NT4;
                InitName    = null;
            }

            var         Names = new List <string>();
            ADSNameType ADSOutputType;

            if (args.Identity != null)
            {
                foreach (var TargetIdentity in args.Identity)
                {
                    if (args.OutputType == null)
                    {
                        if (new Regex(@"^[A-Za-z]+\\[A-Za-z ]+").Match(TargetIdentity).Success)
                        {
                            ADSOutputType = ADSNameType.DomainSimple;
                        }
                        else
                        {
                            ADSOutputType = ADSNameType.NT4;
                        }
                    }
                    else
                    {
                        ADSOutputType = args.OutputType.Value;
                    }
                    var Translate = new ActiveDs.NameTranslate();

                    if (args.Credential != null)
                    {
                        try
                        {
                            Translate.InitEx((int)ADSInitType, InitName, args.Credential.UserName, args.Credential.Domain, args.Credential.Password);
                        }
                        catch (Exception e)
                        {
                            Logger.Write_Verbose($@"[Convert-ADName] Error initializing translation for '{args.Identity}' using alternate credentials : {e}");
                        }
                    }
                    else
                    {
                        try
                        {
                            Translate.Init((int)ADSInitType, InitName);
                        }
                        catch (Exception e)
                        {
                            Logger.Write_Verbose($@"[Convert-ADName] Error initializing translation for '{args.Identity}' : {e}");
                        }
                    }

                    // always chase all referrals
                    Translate.ChaseReferral = 0x60;

                    try
                    {
                        // 8 = Unknown name type -> let the server do the work for us
                        Translate.Set((int)ADSNameType.Unknown, TargetIdentity);
                        Names.Add(Translate.Get((int)ADSOutputType));
                    }
                    catch (Exception e)
                    {
                        Logger.Write_Verbose($@"[Convert-ADName] Error translating '{TargetIdentity}' : {e})");
                    }
                }
            }

            return(Names);
        }