I want to do the user authentication using LDAP(Lightweight Directory Access Protocol).I dont have any knowledge about this.still I have managed to write some code for this,but the problem is that when i am signing with the Username and Password present at my Databse i.e User table I ma not able to login.But when I am using LDAP's Username and password i am able to login into the application My code goes as follows:
public ActionResult Login(APPUser model, string returnUrl)
{
try
{
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain,"10.0.0.100"))
{
if (pc.ValidateCredentials(model.UserID, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserID, false);
return RedirectToAction("Index", "Home");
}
}
if (Membership.ValidateUser(model.UserID, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserID, false);
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "Login failed");
}
}
catch
{
}
//GetErrorsFromModelState();
return View(model);
}
and Web.Config
<connectionStrings>
<add name="ADConnectionString" connectionString="LDAP://"XXXXXXX":389/DC=XXXX,DC=XXX" />
</connectionStrings>
<system.web>
<authentication mode="Forms">
<forms name=".ADAuthCookie" loginUrl="~/Auth/Login" timeout="2880"/>
</authentication>
<membership defaultProvider="ADMembershipProvider">
<providers>
<clear />
<add name="ADMembershipProvider"
type="System.Web.Security.ActiveDirectoryMembershipProvider,System.Web, Version=2.0.0.0,Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
connectionStringName="ADConnectionString"
connectionProtection="Secure"
connectionUsername="admin"
connectionPassword="admin234"
attributeMapUsername="sAMAccountName"
enableSearchMethods="false" />
</providers>
</membership>
Please try to help me out.