title: How to authenticate users in WebServices with Key using different checks
body: <p>I'm working with a Web Api which is using Active Directory to authenticate users. I'm using this Api in PHP to login user that are in my WEB API key (The key is in "Web.Config" file). I'm new to c# web api and there is something wrong which I'm unable to resolve. I need to insert some checks for user. First one is If user is not in the key "Return Not Allowed". If user is in the key but the username or password is invalid "Return Invalid UserName or Password" and if the user input is true then log the user into the system.</p>
  
  <pre><code>public string IsAuthenticated(string user, string pass, string domain)
          {
              DirectoryEntry objDirEntry = new DirectoryEntry("LDAP://" + domain, user, pass);
              try
              {
                  string UserName = user.ToString();
                  string Password = pass.ToString();
                  string Domain = "Netsolpk";
                  DirectorySearcher search = new DirectorySearcher(objDirEntry);
                  SearchResult result = search.FindOne();
                  if (result == null)
                      return "You're not in the Domain";
                  string AdAuthentication = IsAuthenticated(UserName, Password, Domain);
                  string[] name = ConfigurationManager.AppSettings["name"].Split(',');
                  string authorized = "false";
                  foreach (var author in name)
                  {
                      if (AdAuthentication == "true" &amp;&amp; author.ToLower() == user.ToLower())
                      {
                          return "true"; //login
                      }
                      else if (author.ToLower() == user.ToLower())
                      {
                          return "Invalid Password";
                          authorized = "true";
                          break;
                      }
                  }
                  if (AdAuthentication == "false")
                  {
                       return "Not Allowed";
                  }
                  return "false";
              }
              catch (Exception)
              {
                  return "false";
              }
          }
  
  </code></pre>
  
  <pre><code>&lt;appSettings&gt;
      &lt;add key="webpages:Version" value="3.0.0.0"/&gt;
      &lt;add key="webpages:Enabled" value="false"/&gt;
      &lt;add key="ClientValidationEnabled" value="true"/&gt;
      &lt;add key="UnobtrusiveJavaScriptEnabled" value="true"/&gt;
      &lt;add key="name" value="DavidR,JohnH"/&gt;
    &lt;/appSettings&gt;
  </code></pre>