title: How to Implement Active Directory Authentication in ASP.NET Web API Through Forms?
body: <p>I've been reading a lot of guides/articles but haven't found one yet that does exactly what I want... that is to implement Active Directory Authentication in an ASP.NET Web API through forms. </p>
  
  <p>Something like on this guide:</p>
  
  <p><a href="http://www.schiffhauer.com/mvc-5-and-active-directory-authentication/" rel="nofollow">Cool MVC 5 guide to implement authentication with Active Directory</a></p>
  
  <p>Which is very good but it's for MVC, i.e., it uses a Controller not an ApiController</p>
  
  <p>Can someone please give me hints/tips/articles on how to start? Especially about the part that connects to the active directory. I've been stuck on this for a while.</p>
  
  <p>UPDATE:</p>
  
  <pre><code>public bool IsAuthenticatedUser(string srvr, string usr, string password)
         {
             bool authenticated = false;
  
             try {
                 DirectoryEntry entry = new DirectoryEntry(srvr, usr, password);
                 object nativeObject = entry.NativeObject;
                 Object obj = entry.NativeObject;
                 authenticated = true;
             }
             catch {
                 throw new HttpResponseException(HttpStatusCode.Unauthorized);
             }
             return authenticated;
         }
  
         // POST: api/Login
         public void Post([FromBody]string username, [FromBody]string password)
         {
             if (IsAuthenticatedUser("LDAP string", username, password))
             {
                 Redirect("Index");
             }
             else
             {
                 throw new HttpResponseException(HttpStatusCode.Unauthorized);
             }
         }
  </code></pre>
  
  <p>I was thinking of trying something like this for the authentication, your thoughts?</p>