title: LDAP auth in ASP.NET MVC authentication error: "Default credentials are not supported when the connection protection is set to None"
body: <p>I am trying to implement LDAP authentication on the ADAM LDAP server of my company with ASP.NET MVC.</p>
  
  <p>I already tested coordinates and credentials with other LDAP clients and they work fine.
  The server is not SSL secured and it works on standard 389 port, so I set connectionProtection="None" in web.config membership provider.</p>
  
  <p>When I try login with default login page, I get this <strong>error: "Default credentials are not supported when the connection protection is set to None"</strong>.</p>
  
  <p><strong>Here are parts of web.config</strong></p>
  
  <pre><code>&lt;configuration&gt;
    &lt;connectionStrings&gt;
      &lt;add name="ADConnectionString"    connectionString="LDAP://mycompanyldapserver.com:389"/&gt;
  
    ....
  
  &lt;membership defaultProvider="DefaultMembershipProvider"&gt;
    &lt;providers&gt;
      &lt;add name="DefaultMembershipProvider"
           type="System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=2.0.0.0,Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
           connectionStringName="ADConnectionString"
           connectionProtection="None"
           attributeMapUsername="sAMAccountName"
           enableSearchMethods="True"/&gt;
    &lt;/providers&gt;
  &lt;/membership&gt;
  </code></pre>
  
  <p><strong>AccountController</strong></p>
  
  <pre><code>    [AllowAnonymous]
      [HttpPost]
      public JsonResult JsonLogOn(LogOnModel model, string returnUrl)
      {
          if (ModelState.IsValid)
          {
              // &gt;&gt; Membership.ValidateUser THROWS EXCEPTION AFTER LOGIN TRY &gt;&gt;
              if (Membership.ValidateUser(model.UserName, model.Password)) 
              {
                  FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                  return Json(new { success = true, redirect = returnUrl });
              }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>I discovered that the exception "Default credentials are not supported when the connection protection is set to None" is not thrown when I add these parameters to DefaultMembershipProvider:</p>
  
  <pre><code>connectionUsername="username"
  connectionPassword="userpwd"
  </code></pre>
  
  <p>The problem is that username and password must change runtime, based on login form...may you help me to set these parameters runtime, please? 
  I tried to implement this solution, which I suppose could work...I suppose because now I get a "wrong username or password" error instead of above exception specified in the subject. I think I am a step away to solve this problem, anyway I need help from some expert. I do not konw if the following solution can work properly to validate user:</p>
  
  <p><strong>MODIFIED ACCOUNT CONTROLLER: I AM TRYING TO ADD MEMBERSHIP PROVIDER RUNTIME</strong></p>
  
  <pre><code>    [AllowAnonymous]
      [HttpPost]
      public JsonResult JsonLogOn(LogOnModel model, string returnUrl)
      {
  
          // I'm trying to add membership provider runtime...
  
          System.Web.Security.ActiveDirectoryMembershipProvider mprov = new System.Web.Security.ActiveDirectoryMembershipProvider();
  
          NameValueCollection objConfig = new NameValueCollection();
          objConfig.Add("name", "DefaultMembershipProvider");
          objConfig.Add("type", "System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=2.0.0.0,Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
          objConfig.Add("connectionStringName", "ADConnectionString");
          objConfig.Add("connectionProtection", "None");
          objConfig.Add("connectionUsername", model.UserName);
          objConfig.Add("connectionPassword", model.Password);
          objConfig.Add("attributeMapUsername", "sAMAccountName");
          objConfig.Add("enableSearchMethods", "True");
  
          try
          {
              // Here I get "wrong username or password error...wrong DN base? I do not know.....
                   mprov.Initialize(objConfig["name"], objConfig);
                  FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                  return Json(new { success = true, redirect = returnUrl });
          }
  
          catch (System.Runtime.InteropServices.COMException ex)
          {
              // For now I am catching all COMExceptions...if it works I could change behavior...
              ModelState.AddModelError("", ex.Message);
  
          }
  </code></pre>
  
  <p>Thanks in advance for your precious help.</p>