locked
AD queries very slow RRS feed

  • Question

  • Hi all,

    I have a simple script to queries user information.

    private void GetDefaultUserInfo()
    		{
    			string domainName, userName;
    			string[] userPath = WindowsIdentity.GetCurrent().Name.Split(new char[] { '\\' });
    
    			domainName = userPath[0];
    			userName = userPath[1];
    
    			textBoxUserInfo.Text = "";
    			DirectoryEntry rootDse = new DirectoryEntry("LDAP://rootDSE");
    			string strLog = "";
    
    			DirectoryEntry defaultNamingContext = new DirectoryEntry("LDAP://" + rootDse.Properties["defaultNamingContext"][0].ToString());
    
    			DirectorySearcher directorySearcher = new DirectorySearcher(defaultNamingContext, "(sAMAccountName=" + userName + ")");
    
    			SearchResult searchResults = directorySearcher.FindOne();
    			DirectoryEntry ldapUserEntry = new DirectoryEntry(searchResults.Path);
    			ldapUserEntry.RefreshCache(new string[] { "tokenGroups" });
    
    			SecurityIdentifier userSID = new SecurityIdentifier((byte[])ldapUserEntry.Properties["objectSID"].Value, 0);
    
    
    			foreach (byte[] groupSid in ldapUserEntry.Properties["tokenGroups"])
    			{
    				SecurityIdentifier sID = new SecurityIdentifier(groupSid, 0);
    
    				NTAccount account = (NTAccount)sID.Translate(typeof(NTAccount));
    				string sGroup = account.ToString();
    
    				strLog += string.Format("{0}  [SID: {1}]\r\n", sGroup, sID);
    			}
    
    			textBoxUserInfo.Text = strLog;
    		}

    This script execute OK on some machines in our network, but it very slow on another machine. The source code that is slow is NTAccount account = (NTAccount)sID.Translate(typeof(NTAccount));

    Do you known what is the problem of that machine and is there any workaround for that?

    Thank you


    Wednesday, February 12, 2014 10:17 AM

Answers