コード例 #1
0
ファイル: LDapYonetici.cs プロジェクト: tbalci/GalataSozluk
 public List<string> KullaniciBilgileriniVer(string KullaniciAd,string Sifre)
 {
     List<string> Sonuc = new List<string>();
     try
     {
         DirectoryEntry objDe = new DirectoryEntry(Ldap, KullaniciAd, Sifre);
         DirectorySearcher searcher = new DirectorySearcher(objDe);
         searcher.Filter = "(&(objectClass=person)(sAMAccountName=" + KullaniciAd + "))";
         searcher.PropertiesToLoad.Add("givenname");
         searcher.PropertiesToLoad.Add("sn");
         searcher.PropertiesToLoad.Add("userPrincipalName");
         SortOption Srt;
         Srt = new SortOption("sn", System.DirectoryServices.SortDirection.Ascending);
         searcher.Sort = Srt;
         SearchResult Result = searcher.FindOne();
         DirectoryEntry userEntry = null;
         userEntry = Result.GetDirectoryEntry();
         if (userEntry != null)
         {
             Sonuc.Add(KullaniciAd);
             Sonuc.Add( userEntry.Properties["givenname"].Value.ToString());
             Sonuc.Add( userEntry.Properties["sn"].Value.ToString());
             Sonuc.Add( userEntry.Properties["userPrincipalName"].Value.ToString());
         }
         else
         {
             Sonuc = new List<string>();
         }
     }
     catch (Exception Hata)
     {
         Sonuc = new List<string>();
     }
     return Sonuc;
 }
コード例 #2
0
        public IEnumerable<UserInSource> GetUsers()
        {
            var entry = new DirectoryEntry(_ldapServer, _username, _password);

            entry.AuthenticationType = AuthenticationTypes.None;
            var searcher = new DirectorySearcher(entry);

            searcher.Filter = _ldapSearchFilter;
            searcher.SearchScope = SearchScope.Subtree;

            var option = new SortOption(_nameProperty, SortDirection.Ascending);
            searcher.Sort = option;
            searcher.PageSize = 500;
            int count = 0;

            IList<UserInSource> users = new List<UserInSource>();
            foreach (SearchResult resEnt in searcher.FindAll())
            {

                //Trace.TraceInformation("Loading User #"+count);
                count++;
                DirectoryEntry de = resEnt.GetDirectoryEntry();
                switch (de.SchemaClassName)
                {
                    case "group":
                        break;
                    case "user":
                        {
                            String account = Convert.ToString(de.Properties[_nameProperty].Value);
                            String displayName = Convert.ToString(de.Properties["displayName"].Value);
                            String sn = Convert.ToString(de.Properties["sn"].Value);
                            String fn = Convert.ToString(de.Properties["givenName"].Value);
                            Int32 userAccountControl = Convert.ToInt32(de.Properties["userAccountControl"].Value);
                            var flags = (LdapFlags)userAccountControl;
                            bool isEnabled = (flags & LdapFlags.AccountDisabled) == 0;

                            // get the group from Ldap and assign it to the user
                            var newUser = new UserInSource();
                            //newUser.LoginName = account;
                            // fn, sn, isEnabled;
                            users.Add(newUser);
                        }
                        break;
                    case "computer":
                        break;
                    default:
                        break;
                }
            }

            return users;
        }
コード例 #3
0
        /// <summary>
        /// Get groups from a specified domain
        /// </summary>
        private void GetADGroups(string strDomain)
        {
            try
            {
                System.DirectoryServices.DirectoryEntry entry = new System.DirectoryServices.DirectoryEntry("LDAP://" + strDomain);
                entry.AuthenticationType = AuthenticationTypes.Secure;
                System.DirectoryServices.DirectorySearcher mySearcher =
                    new System.DirectoryServices.DirectorySearcher(entry);
                mySearcher.Filter = "(&(objectCategory=group))";

                System.DirectoryServices.SortOption sortOption = new
                                                                 System.DirectoryServices.SortOption("name",
                                                                                                     System.DirectoryServices.SortDirection.Ascending);
                mySearcher.Sort = sortOption;

                lstGroups.Items.Clear();
                AccessManager objAccess = new AccessManager();
                DataTable     objTable  = objAccess.GetGroups();
                objTable.DefaultView.Sort = "GroupDesc";

                foreach (System.DirectoryServices.SearchResult resEnt in mySearcher.FindAll())
                {
                    try
                    {
                        System.DirectoryServices.DirectoryEntry de = resEnt.GetDirectoryEntry();

                        // Get group name
                        string strGroupName = de.Properties["name"].Value.ToString();
                        // Verify if group allready exists on OfficeWorks
                        int RowNum = objTable.DefaultView.Find(strGroupName);

                        // If group does no exists
                        if (RowNum == -1)
                        {
                            lstGroups.Items.Add(strGroupName);
                        }
                    }
                    catch (Exception ex)
                    {
                        OfficeWorksException.WriteEventLog(ex.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                OfficeWorksException.WriteEventLog(ex.Message);
            }
        }
 public DirectorySearcher(DirectoryEntry searchRoot, string filter, string[] propertiesToLoad, System.DirectoryServices.SearchScope scope)
 {
     this.filter = "(objectClass=*)";
     this.scope = System.DirectoryServices.SearchScope.Subtree;
     this.serverTimeLimit = minusOneSecond;
     this.clientTimeout = minusOneSecond;
     this.serverPageTimeLimit = minusOneSecond;
     this.referralChasing = ReferralChasingOption.External;
     this.sort = new SortOption();
     this.cacheResults = true;
     this.attributeScopeQuery = "";
     this.extendedDN = System.DirectoryServices.ExtendedDN.None;
     this.searchRoot = searchRoot;
     this.filter = filter;
     if (propertiesToLoad != null)
     {
         this.PropertiesToLoad.AddRange(propertiesToLoad);
     }
     this.SearchScope = scope;
 }
コード例 #5
0
        public static SearchResultCollection QueryAD(string ldapPath, string ldapFilter, string searchScope, string[] ldapProperties)
        {
            if (ldapPath == null)
            {
                DirectoryEntry myDir = new DirectoryEntry("LDAP://RootDSE");
                object propertyValue = myDir.Properties["defaultNamingContext"].Value;
                ldapPath = propertyValue.ToString();
                Console.WriteLine(myDir.Properties["defaultNamingContext"].Value);
                myDir.Dispose();
            }

            if (!(ldapPath.ToUpper().Contains("LDAP://")))
            {
                ldapPath = "LDAP://" + ldapPath;
            }

            try
            {
                // Creating new DirectoryEntry object
                DirectoryEntry dirEntry = new DirectoryEntry(ldapPath.ToUpper());
                // Creating new DirectorySearcher
                DirectorySearcher dirSearcher = new DirectorySearcher(ldapFilter);

                // Sort in ascending order
                SortOption sortOption = new SortOption("Name", System.DirectoryServices.SortDirection.Ascending);

                // Setting SearchRoot
                dirSearcher.SearchRoot = dirEntry;
                // Setting PageSize to 1000
                dirSearcher.PageSize = 1000;
                // Setting dirSearcher filter
                dirSearcher.Filter = ldapFilter;

                // Setting dirSearch sort
                dirSearcher.Sort = sortOption;

                switch (searchScope)
                {
                    case "Base":
                        {
                            // Setting Searchscope to Base
                            dirSearcher.SearchScope = SearchScope.Base;
                            break;
                        }
                    case "OneLevel":
                        {
                            // Setting Searchscope to OneLevel
                            dirSearcher.SearchScope = SearchScope.OneLevel;
                            break;
                        }
                    case "Subtree":
                        {
                            // Setting Searchscope to Subtree
                            dirSearcher.SearchScope = SearchScope.Subtree;
                            break;
                        }
                } // End Switch

                if (ldapProperties != null)
                {
                    // Load properties into the query
                    foreach (string AdProperty in ldapProperties)
                    {
                        // Loading property
                        dirSearcher.PropertiesToLoad.Add(AdProperty);
                    }
                }
                dirEntry.Dispose();
                SearchResultCollection queryResult;
                queryResult = dirSearcher.FindAll();
                return queryResult;
            }
            catch
            {
                throw;
            }
            finally
            {
            }
        }
コード例 #6
0
ファイル: ADHelper.cs プロジェクト: jerryshi2007/AK47Source
 public ADSearchConditions(SortOption sort)
 {
     _Sort = sort;
 }
コード例 #7
0
ファイル: LDapYonetici.cs プロジェクト: tbalci/GalataSozluk
 public bool SifreDegisitir(string KulAd, string EskiSifre, string YeniSifre)
 {
     bool Sonuc = false;
     DirectoryEntry objDe = new DirectoryEntry(Ldap, KulAd, EskiSifre);
     DirectorySearcher searcher = new DirectorySearcher(objDe);
     searcher.Filter = "(&(objectClass=person)(sAMAccountName=" + KulAd + "))";
     searcher.PropertiesToLoad.Add("cn");
     searcher.PropertiesToLoad.Add("sn");
     SortOption Srt;
     Srt = new SortOption("sn", System.DirectoryServices.SortDirection.Ascending);
     searcher.Sort = Srt;
     SearchResult Result = searcher.FindOne();
     DirectoryEntry userEntry = null;
     userEntry = Result.GetDirectoryEntry();
     try
     {
         if (userEntry != null)
         {
             userEntry.Invoke("ChangePassword", new object[] { EskiSifre, YeniSifre });
             userEntry.CommitChanges();
             Sonuc = true;
         }
         else
         {
             Sonuc = false;
         }
     }
     catch (Exception Hata)
     {
         Sonuc = false;
     }
     return Sonuc;
 }