title: Active Directory in Asp.net C#
body: <p>I have one problem regarding to Active Directory. My project has been hosted in one server. And the active directory has been maintained in another server. Now I need the AD authientication in my application while employee login. I am totally confused while using this two different serves since I could not fetch the records of ACtive Directory; the code I have used is:</p>
  
  <pre><code>string principal = this.Context.User.Identity.Name;
  string filter = string.Format("(&amp;(ObjectClass=dev)(sAMAccountName={1}))", "dev", principal);
  string domain = "SOFTWARESERVER";
  string[] properties = new string[] { "fullname","mail","sn" };
  System.Security.Principal.WindowsIdentity wi = System.Security.Principal.WindowsIdentity.GetCurrent();
  string[] a = Context.User.Identity.Name.Split('\\');
  
  DirectoryEntry ADEntry = new DirectoryEntry("LDAP://SOFTWARESERVER/DC=softageenapl,DC=com,DC=np");
  DirectorySearcher searcher = new DirectorySearcher(ADEntry);
  
  searcher.SearchScope = SearchScope.Subtree;
  searcher.ReferralChasing = ReferralChasingOption.All;
  searcher.PropertiesToLoad.AddRange(properties);
  searcher.Filter = filter;
  SearchResult result = searcher.FindOne();
  DirectoryEntry directoryEntry = result.GetDirectoryEntry();
  
  string Name = ADEntry.Properties["Fullname"].Value.ToString();
  string displayName = directoryEntry.Properties["displayName"][0].ToString();
  string firstName = directoryEntry.Properties["givenName"][0].ToString();
  string lastName = directoryEntry.Properties["sn"][0].ToString();
  string email = directoryEntry.Properties["mail"][0].ToString();
  </code></pre>