how to implement LDAP Authentication in Login Page using C# mvc
Loading
how to implement LDAP Authentication in Login Page using C# mvc
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 16, 2023, 5:43 AM EDT
Hello Neeraj,
Basic steps to implement LDAP authentication:
1. Add a reference to the System.DirectoryServices assembly in your project.
2. Modify the web.config file to include the following configuration settings
3. In the Login action of your controller, use the DirectoryEntry class to authenticate the user
[HttpPost] public ActionResult Login(LoginViewModel model, string returnUrl) { if (ModelState.IsValid) { try { using (var entry = new DirectoryEntry("LDAP://yourdomain.com", model.UserName, model.Password)) { var search = new DirectorySearcher(entry) { Filter = "(&(objectClass=user)(sAMAccountName=" + model.UserName + "))" }; SearchResult result = search.FindOne(); if (result != null) { // User authenticated successfully, redirect to the home page FormsAuthentication.SetAuthCookie(model.UserName, false); return RedirectToAction("Index", "Home"); } } } catch (Exception ex) { ModelState.AddModelError("", "An error occurred while trying to authenticate. Please try again later."); } } // Authentication failed, show the login page again return View(model); }4. Create a Login view that includes fields for the username and password.
Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 16, 2023, 4:48 AM EDT
Hello Neeraj,
Please check below link:
https://www.c-sharpcorner.com/article/ldap-access-control-for-Asp-Net/
https://nicolas.guelpa.me/blog/2017/02/15/dotnet-core-ldap-authentication.html
Thanks
Naimish Makwana
neeraj kumar107 reputationPosted Mar 16, 2023, 7:15 AM EDT
Username will search in apache directory studio or else please explain this confusion.
& plz tell me
What to write instead of ldp://yourdomain.com
&
what will be in the connection username