/// <summary>
 /// IsBinaryAttribute method implmentation
 /// </summary>
 internal static bool IsBinaryAttribute(string domainname, string username, string password, string attributename)
 {
     try
     {
         using (Domain domain = ADDSUtils.GetRootDomain(domainname, username, password))
         {
             using (Forest forest = ADDSUtils.GetForest(domain.Name, username, password))
             {
                 ActiveDirectorySchemaProperty property = forest.Schema.FindProperty(attributename);
                 if (property != null)
                 {
                     if (property.Syntax.Equals(ActiveDirectorySyntax.OctetString))
                     {
                         return(true);
                     }
                 }
             }
         }
         return(false);
     }
     catch (Exception ex)
     {
         DataLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error, 5100);
         return(false);
     }
 }
        /// <summary>
        /// GetDirectoryEntryForUPN() method implmentation
        /// </summary>
        internal static DirectoryEntry GetDirectoryEntryForUser(ADDSHost host, string account, string password, string upn)
        {
            string         root  = "LDAP://";
            DirectoryEntry entry = null;
            string         dom   = ADDSUtils.GetForestForUser(host, upn);

            if (_usessl)
            {
                entry = new DirectoryEntry(root + dom + ":636");
            }
            else
            {
                entry = new DirectoryEntry(root + dom);
            }

            if (!string.IsNullOrEmpty(account))
            {
                entry.Username = account;
            }
            if (!string.IsNullOrEmpty(password))
            {
                entry.Password = password;
            }
            return(entry);
        }
Пример #3
0
        /// <summary>
        /// GetMFAdistinguishedName method implementation
        /// </summary>
        private string GetMFAdistinguishedName(string upn)
        {
            string ret = string.Empty;

            try
            {
                using (DirectoryEntry rootdir = ADDSUtils.GetDirectoryEntryForUPN(_host, _host.Account, _host.Password, upn))
                {
                    string qryldap = "(&(objectCategory=user)(objectClass=user)(userprincipalname=" + upn + ")(!(userAccountControl:1.2.840.113556.1.4.803:=2)))";
                    using (DirectorySearcher dsusr = new DirectorySearcher(rootdir, qryldap))
                    {
                        dsusr.PropertiesToLoad.Add("objectGUID");
                        dsusr.PropertiesToLoad.Add("userPrincipalName");
                        dsusr.PropertiesToLoad.Add("whenCreated");
                        dsusr.PropertiesToLoad.Add("distinguishedName");

                        SearchResult sr = dsusr.FindOne();
                        if (sr != null)
                        {
                            using (DirectoryEntry DirEntry = ADDSUtils.GetDirectoryEntry(_host, sr))
                            {
                                ret = DirEntry.Properties["distinguishedName"].Value.ToString();
                            };
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                DataLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error, 5000);
                throw new Exception(ex.Message);
            }
            return(ret);
        }
 /// <summary>
 /// LoadForests method implementation
 /// </summary>
 public static void LoadForests(string domainname, string account, string password, bool usessl, bool reset = false)
 {
     if (reset)
     {
         ResetForests();
     }
     if (_isbinded)
     {
         return;
     }
     try
     {
         _usessl = usessl;
         using (Domain domain = ADDSUtils.GetRootDomain(domainname, account, password))
         {
             using (Forest forest = ADDSUtils.GetForest(domain.Name, account, password))
             {
                 Forests.Clear();
                 ADDSHostForest root = new ADDSHostForest
                 {
                     IsRoot    = true,
                     ForestDNS = forest.Name
                 };
                 Forests.Add(root);
                 foreach (ForestTrustRelationshipInformation trusts in forest.GetAllTrustRelationships())
                 {
                     ADDSHostForest sub = new ADDSHostForest
                     {
                         IsRoot    = false,
                         ForestDNS = trusts.TargetName
                     };
                     foreach (TopLevelName t in trusts.TopLevelNames)
                     {
                         if (t.Status == TopLevelNameStatus.Enabled)
                         {
                             sub.TopLevelNames.Add(t.Name);
                         }
                     }
                     Forests.Add(sub);
                 }
             }
         }
         _isbinded = true;
     }
     catch (Exception ex)
     {
         DataLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error, 5100);
         _isbinded = false;
     }
 }
        /// <summary>
        /// GetNetBiosName method
        /// </summary>
        private static string GetNetBiosName(ADDSHost host, string username)
        {
            try
            {
                using (DirectoryEntry rootdir = ADDSUtils.GetDirectoryEntryForUser(host, host.Account, host.Password, username))
                {
                    string qryldap = "(&(objectCategory=user)(objectClass=user)(userPrincipalName=" + username + ")(!(userAccountControl:1.2.840.113556.1.4.803:=2)))";
                    using (DirectorySearcher dsusr = new DirectorySearcher(rootdir, qryldap))
                    {
                        dsusr.PropertiesToLoad.Clear();
                        dsusr.PropertiesToLoad.Add("objectGUID");
                        dsusr.PropertiesToLoad.Add("msDS-PrincipalName");
                        dsusr.ReferralChasing = ReferralChasingOption.All;

                        SearchResult sr = dsusr.FindOne();
                        if (sr != null)
                        {
                            using (DirectoryEntry DirEntry = ADDSUtils.GetDirectoryEntry(host, sr))
                            {
                                if (DirEntry.Properties["objectGUID"].Value != null)
                                {
                                    return(sr.Properties["msDS-PrincipalName"][0].ToString());
                                }
                                else
                                {
                                    return(null);
                                }
                            };
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                DataLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error, 5000);
                throw new Exception(ex.Message);
            }
            return(null);
        }