title: Get list of users from trusted domain
body: <p>How do I get users list from trusted domain? </p>
  
  <p>I have tried to run a LDAP query but I am not able to get users from trusted domain. This is my code:</p>
  
  <pre><code>public virtual List&lt;UserModel&gt; SearchUsers(string textValue)
  {
      var users = new List&lt;UserModel&gt;();
      string context;
      const string nameProperty = "name";
      const string samAccountNameProperty = "samaccountname";
      const string distinguishedNameProperty = "distinguishedname";
  
      if (textValue.Contains("(").Equals(true) || textValue.Contains(")").Equals(true) || textValue.Contains("*").Equals(true))
      {
          textValue = EscapeInvalidCharacters(textValue);
      }
  
      var filterForDomainUser ="(&amp;(objectClass=user)(!(userAccountControl:1.2.840.113556.1.4.803:=2))(|(samaccountname=" + textValue + "*)(name=" + textValue + "*)))";
  
      using (HostingEnvironment.Impersonate())
      {
          using (var root = new DirectoryEntry("LDAP://RootDSE"))
          {
              context = root.Properties["defaultNamingContext"].Value.ToString();
          }
  
          using (var entry = new DirectoryEntry("GC://" + context))
          {
              using (
              var search = new DirectorySearcher(entry,filterForDomainUser,
                                                  new[]
                                                      {
                                                          samAccountNameProperty, nameProperty,
                                                          distinguishedNameProperty
                                                      }, SearchScope.Subtree))
              {
                  search.ReferralChasing = ReferralChasingOption.All;
                  search.PageSize = 10;
  
                  var resultCol = search.FindAll();
  
                  for (var counter = 0; counter &lt; resultCol.Count; counter++)
                  {
                      var result = resultCol[counter];
                      var distinguishedName = (String)result.Properties[distinguishedNameProperty][0];
                      var domainName =
                          distinguishedName.Substring(distinguishedName.IndexOf("DC=",
                                                                                  StringComparison
                                                                                      .InvariantCultureIgnoreCase))
                              .Split(',')[0].Replace("DC=", "");
                      var name = (String)result.Properties[nameProperty][0];
                      var samAccountName = string.Format("{0}{1}{2}", domainName, @"\",
                                                          result.Properties[samAccountNameProperty][0]);
  
                      var userModel = new UserModel
                      {
                          DisplayName = name,
                          UserName = samAccountName
                      };
                      users.Add(userModel);
                  }
              }
          }
      }
  
      //SearchLocalUser(textValue, users);
  
      return users;
  }
  </code></pre>