Exemple #1
0
        public void ResetPassword(string password, AuthenticationOption option, string objectNotFoundExceptionText)
        {
            using (DirectorySearcher searcher = new DirectorySearcher())
            {
                DirectoryEntry searchRoot = new DirectoryEntry();
                searchRoot.Path               = option.BuildLDAPPath();
                searchRoot.Username           = option.ServiceAccountName;
                searchRoot.Password           = option.ServiceAccountPwd;
                searchRoot.AuthenticationType = option.GetAuthenticationType();

                searcher.Filter      = option.BuildSearchFilter();
                searcher.PageSize    = 1000;
                searcher.SearchScope = SearchScope.Subtree;
                searcher.SearchRoot  = searchRoot;

                SearchResultCollection searchResult = searcher.FindAll();

                if (searchResult != null && searchResult.Count == 1)
                {
                    searchResult[0].GetDirectoryEntry().Invoke("SetPassword", new object[] { password });
                }
                else
                {
                    throw new Exception(objectNotFoundExceptionText);
                }
            }
        }
Exemple #2
0
        public bool SetStringValue(string attrName, string attrValue, AuthenticationOption option, SearchResultOption searchOption)
        {
            using (DirectorySearcher searcher = new DirectorySearcher())
            {
                try
                {
                    DirectoryEntry searchRoot = new DirectoryEntry();
                    searchRoot.Path               = option.BuildLDAPPath();
                    searchRoot.Username           = option.ServiceAccountName;
                    searchRoot.Password           = option.ServiceAccountPwd;
                    searchRoot.AuthenticationType = option.GetAuthenticationType();

                    searcher.Filter   = option.BuildSearchFilter();
                    searcher.PageSize = 1000;
                    searcher.PropertiesToLoad.Add(attrName);
                    searcher.SearchScope = SearchScope.Subtree;
                    searcher.SearchRoot  = searchRoot;

                    SearchResultCollection searchResult = searcher.FindAll();

                    switch (searchOption)
                    {
                    case SearchResultOption.FindTheFirstOne:
                        if (searchResult != null && searchResult.Count > 0)
                        {
                            DirectoryEntry de = searchResult[0].GetDirectoryEntry();
                            de.Properties[attrName].Value = attrValue;
                            de.CommitChanges();
                        }
                        break;

                    case SearchResultOption.FindTheOnlyOne:
                        if (searchResult != null && searchResult.Count == 1)
                        {
                            DirectoryEntry de = searchResult[0].GetDirectoryEntry();
                            de.Properties[attrName].Value = attrValue;
                            de.CommitChanges();
                        }
                        else
                        {
                            return(false);
                        }
                        break;

                    case SearchResultOption.FindAll:
                    default:
                        throw new NotImplementedException();
                    }
                }
                catch (Exception)
                {
                    return(false);
                }
            }

            return(true);
        }