我的代码杀死了Active Directory [英] My Code Kills Active Directory

查看:96
本文介绍了我的代码杀死了Active Directory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的下面的代码,我不确定这是否能与Active Directory连接并有效地提取数据.

每当使用此方法时,它就会显示交换服务器正在运行100%的proc内存.

感谢Advance的帮助,有人建议如何有效地做到这一点

Hi ,

This is my code below,am not sure is this anyway around to connect with Active Directory and pull the data efficiently.

when ever it hits this method it shows up exchange server is running 100% proc memory.

Some one advise how to do this efficiently,thanks in Advance

public void FixUserPhoneNumberfromAD(EmployeeExport item, SqlConnection sqlConnection1)
        {
            string phonenumber = string.Empty; // we will get this value from the AD
            string EmailID = string.Empty;
            string employeeNumber = string.Empty; // we will get this from AD. 
            string UserId = string.Empty; // we will get a user Id from AD.

            try
            {
                
                DirectoryEntry entry = new DirectoryEntry("DomainName");
                DirectorySearcher searcher = new DirectorySearcher();
                searcher.Filter = "(otherpager=" + item.EmployeeNumber + ")";
                foreach (SearchResult result in searcher.FindAll())
                {
                    phonenumber = result.GetDirectoryEntry().Properties["telephonenumber"].Value.ToString();
                    EmailID =     result.GetDirectoryEntry().Properties["mail"].Value.ToString();
                                     
                    entry.Close();
                    if (!String.IsNullOrEmpty(phonenumber))
                    {
                        ///go to the database and update the phone number
                        /// 
                        UpdateDatabasewithPhoneNumber(item.EmployeeNumber, phonenumber, sqlConnection1);
                        UpdateDatabasewithEmail(item.EmployeeNumber, EmailID, sqlConnection1);
                        
                       // entry.Close();
                       
                    }
                    break;
                } 
            }
            catch (System.Exception se) // exception on the console. 
            {
                Console.WriteLine(se.Message);
            }
        }

推荐答案

1.使您的搜索条件更具限制性.例如,由于看起来您只是在搜索人,因此可以设置objectCategory=person等.
2.通过调用searcher.PropertiesToLoad.Add仅请求您使用的属性(电话号码和邮件).
3.将页面大小设置为非零(例如1000).
4.为避免内存泄漏,请不要忘记在对FindAll()的调用返回的结果上调用Dispose().
1. Make your search criteria more restrictive. For example, since it looks like you are searching only for people, you could set objectCategory=person, etc.
2. Request only the properties that you use (telephonenumber and mail) by calling searcher.PropertiesToLoad.Add.
3. Set the page size to something other than zero (say, 1000).
4. To avoid a memory leak, don''t forget to call Dispose() on the results returned from the call to FindAll().


请遵循dasblinkenlight的建议,类似这样的内容可搜索其公用名通配符与字符串匹配的用户.它遵循所有给出的建议,并应证明您需要做的事情的类型

Follow dasblinkenlight''s advice, I use something like this to search for users where their common name wildcard matches a string. It follows all of the advice given and should demonstrate the type of thing you need to do

private List<ActiveDirectoryUser> GetDomainUsers(string name)
{
    var userList = new List<ActiveDirectoryUser>();

    using (var root = new DirectoryEntry(domainName))
    {
        using (var s = new DirectorySearcher(root))
	{
	    s.Filter = String.Format("(&(objectCategory=Person)(cn=*{0}*))", name);

	    s.PropertiesToLoad.Add("cn"); // full name
	    s.PropertiesToLoad.Add("sAMAccountName"); // domain account name
            s.PropertiesToLoad.Add("givenName"); // firstname
	    s.PropertiesToLoad.Add("sn"); // surname

	    using (var results = s.FindAll())
	    {
		for (int i = 0; i < results.Count; i++)
		{
		    SearchResult result = results[i];
		    var user = new ActiveDirectoryUser(result);
			
		    userList.Add(user);
		}
	    }
	}
    }        
    return userList;
}


这篇关于我的代码杀死了Active Directory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆