Integrate LDAP to ASP.NET Web Application

With VS 2017 its very simple, follow the steps.

  1. Create ASP.NET web appcreate_proj.PNG
  2. Select authentication with “Individual User Accounts”create_proj_2.PNG
  3. Change AccountController as below,
  4. [AllowAnonymous]
    [HttpGet]
    public ActionResult Login()
    {
    return View();
    }

    [AllowAnonymous]
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
    if (ModelState.IsValid)
    {
    if (Membership.ValidateUser(model.Email, model.Password))
    {
    FormsAuthentication.SetAuthCookie(model.Email,                   model.RememberMe);
    return RedirectToAction(“Index”, “Home”);
    }
    else
    {
    ModelState.AddModelError(“”, “The user name or password provided is incorrect”);
    }
    }
    model.Password, model.RememberMe, shouldLockout: false);

    return RedirectToLocal(returnUrl);
    }

    private ActionResult RedirectToLocal(string returnUrl)
    {
    if (Url.IsLocalUrl(returnUrl))
    {
    return Redirect(returnUrl);
    }
    return RedirectToAction(“Index”, “Home”);
    }

    public ActionResult LogOff()
    {
    FormsAuthentication.SignOut();

    return RedirectToAction(“Index”, “Home”);
    }

  5. Most important port is configuring the WebConfig file for AD connection
  6. Make sure the Forms Authentication module is available
  7. <modules><modules> <remove name=”FormsAuthentication” /> <add name=”FormsAuthentication” type=”System.Web.Security.FormsAuthenticationModule” /> <remove name=”ApplicationInsightsWebTracking” /> <add name=”ApplicationInsightsWebTracking” type=”Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web” preCondition=”managedHandler” /> </modules>

  8. Add connection string as below,
  9. <connectionStrings><connectionStrings> <add name=”ADConnectionString” connectionString=”LDAP://<your AD server>” /> </connectionStrings>

  10. Authentication mode and membership as blelow,
  11. <authentication mode=”Forms”><authentication mode=”Forms”> <forms name=”.ADAuthCookie” loginUrl=”~/Account/Login” timeout=”45″  slidingExpiration=”false” protection=”All” /> </authentication> <membership defaultProvider=”MyADMembershipProvider”> <providers> <add name=”MyADMembershipProvider” type=”System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a” connectionStringName=”ADConnectionString” connectionUsername=”<AD username>”   connectionPassword=”<ADUserPassword>”  attributeMapUsername=”<value as below 12>” /> </providers> </membership>

12. “attributeMapUsername” can either be “distinguisedName” or “sAMAccountName”

13. That’s all !

Advertisements

About buminda

buminda.com
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s