无法从IIS读取活动目录用户,但可以在调试模式下读取

时间:2015-05-20 14:16:14

标签: c# asp.net ldap iis-6

我在使用Asp.net IIS应用程序的LDAP中遇到了问题。 我只是从Active目录中读取用户,如下所示,

public class ActiveDirectoryRepository : IActiveDirectoryRepository
    {
        #region Public methods

        List<User> IActiveDirectoryRepository.GetActiveDirectoryUsers(string loginName)
        {
            List<User> activeDirectoryUsersList = new List<User>();
            try
            {
                string activeDirectory = System.Configuration.ConfigurationManager.AppSettings["EDPUserDomain"];
                var searchRoot = new DirectoryEntry(activeDirectory);
                var search = new DirectorySearcher(searchRoot);

                FormUserSearchFilter(loginName, search);

                SearchResultCollection activeDirectoryUsers = search.FindAll();

                if (activeDirectoryUsers != null)
                {
                    for (int counter = 0; counter < activeDirectoryUsers.Count; counter++)
                    {
                        string userNameEmailString = string.Empty;
                        SearchResult result = activeDirectoryUsers[counter];

                        if (result != null && result.Properties.Contains("displayname"))
                        {
                            User activeDirectoryUser = new User();

                            if (result.Properties["givenname"].Count > 0)
                            {
                                activeDirectoryUser.FirstName = Convert.ToString(result.Properties["givenname"][0], CultureInfo.InvariantCulture);
                            }

                            if (result.Properties["sn"].Count > 0)
                            {
                                activeDirectoryUser.LastName = Convert.ToString(result.Properties["sn"][0], CultureInfo.InvariantCulture);
                            }

                            if (result.Properties["mail"].Count > 0)
                            {
                                activeDirectoryUser.email = Convert.ToString(result.Properties["mail"][0], CultureInfo.InvariantCulture);
                            }

                            if (result.Properties["distinguishedname"].Count > 0)
                            {
                                string[] domain = Convert.ToString(result.Properties["distinguishedname"][0], CultureInfo.InvariantCulture).Split(',');

                                if (domain[2] != null)
                                {
                                    activeDirectoryUser.DomainName = domain[2].Replace("DC=", String.Empty);
                                }
                            }

                            if (result.Properties["samaccountname"].Count > 0)
                            {
                                activeDirectoryUser.UserName = Convert.ToString(result.Properties["samaccountname"][0], CultureInfo.InvariantCulture);
                            }

                            activeDirectoryUsersList.Add(activeDirectoryUser);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string filePath = @"C:\Mails\Error.txt";

                using (StreamWriter writer = new StreamWriter(filePath, true))
                {
                    writer.WriteLine("Message :" + ex.Message + "<br/>" + Environment.NewLine + "StackTrace :" + ex.StackTrace +
                       "" + Environment.NewLine + "Date :" + DateTime.Now.ToString());
                    writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
                }
            }

            return activeDirectoryUsersList;
        }

以上代码在调试模式下运行良好,但在IIS 6.1中部署应用程序后会抛出以下错误

Message :An operations error occurred.
<br/>
StackTrace :   at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
   at System.DirectoryServices.DirectoryEntry.Bind()
   at System.DirectoryServices.DirectoryEntry.get_AdsObject()
   at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)
   at System.DirectoryServices.DirectorySearcher.FindAll()
   at PW.EPD.DataAccess.Repository.ActiveDirectoryRepository.PW.EPD.DataAccess.Repository.IActiveDirectoryRepository.GetActiveDirectoryUsers(String loginName)
Date :5/20/2015 11:50:30 PM

-----------------------------------------------------------------------------

请帮帮我。 提前谢谢。

0 个答案:

没有答案