Login Using Active Directory in Asp.net Mvc 5?

I am new for Active Directory authentication. Need to create login using active directory. Please help me with explained example or link where I can learn to create active directory login

My web.config file:

  <authentication mode="Forms">
  <forms name=".ADAuthCookie" loginUrl="~/Service/Index" timeout="45" slidingExpiration="false" protection="All" />
</authentication>

      <trust level="Full" originUrl="" />
<membership defaultProvider="ADMembershipProvider">
  <providers>
    <clear />
     <add name="ADMembershipProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a " 
          connectionStringName="ADConnectionString" 
          connectionProtection="Secure" 
          connectionUsername="bosuser10" 
          connectionPassword="[email protected]"
          attributeMapUsername="sAMAccountName" />
  </providers>
</membership>

<connectionStrings>
      <add name="ADConnectionString" connectionString="LDAP://fontier.bos.com:389/DC=fontier,DC=bos,DC=com" />
  </connectionStrings>

And my login action method is:

[HttpPost]
    [AllowAnonymous]
    public ActionResult LoginUser(LoginUser login, string returnUrl)
    {
        if (ModelState.IsValid)
        {                     
           
            //MembershipProvider domainProvider = Membership.Providers["ADMembershipProvider"];
            //if (domainProvider.ValidateUser(login.UserName, login.Password))
            if (Membership.ValidateUser(login.UserName, login.Password))
            {
                FormsAuthentication.SetAuthCookie(login.UserName,true);
                if(Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/"))
                {
                    return Redirect(returnUrl);
                }
                else
                {
                    return RedirectToAction("Index", "Service");
                }
            }
        }
        else
        {
            ModelState.AddModelError("", "The user name or password provided is incorrect");
        }

        return View(login);
    }

Logout action:

public ActionResult Logout()
    {
        
        FormsAuthentication.SignOut();
        return RedirectToAction("LoginUser", "User");
    }
       

Here, I am getting error : The specified domain or server could not be contacted.
I googled a lot but couldn’t get anything useful….Please give your valuable suggestion for this….Thanks.

Source: Windows Questions

LEAVE A COMMENT