Introduction
In this article, I will provide information on how to get user suggestions in a people picker control by querying the LDAP using Novell Directory Nuget Package in a Razor pages. In order to see how to create a .Net Core Web Application with Razor Pages and retrieve data from SQL Server using Entity Framework, you can visit my
previous article.
Below are the software/concepts used in this document.
- Visual Studio 2019
- Razor Pages
- .Net Core 2.1
- Net Core Web Application
- C# Language
- Novell Directory Nuget Package
Many times we get a requirement where the user wants to search the LDAP in a text control and get suggestions from LDAP. This requirement can be handled in .Net Core by creating a people picker-like control and querying the LDAP using the Novell Directory Nuget Package. Below is a step-by-step description of how to achieve this in .Net Core Razor Pages.
Open your project in Visual Studio 2019
In my case, I am opening the earlier-created project where Razor pages are present.
People Picker control code in cshtml page of .Net Core Web Application
Insert the below code to have the people picker control on the cshtml page.
In my example, I will be storing the value of people picker in “strGateKeeper1”
- <tr>
- <td colspan="1" class="form-label">
- <div class="rTableCell-label">
- <label asp-for="structure.strGateKeeper1"></label>
- </div>
- </td>
- <td colspan="1" class="control">
- <div class="rTableCell-control">
- <input asp-for="structure.strGateKeeper1" />
- <input asp-for="structure.Email_GateKeeper1" type="hidden" />
- </div>
- </td>
- </tr>
Add the below line in the script section of the same cshtml code where you inserted above code. The below code would call the Javascript method for the people picker autocomplete and call the controller method as well. In my example: “structure_strGateKeeper1” is the control name and “/api/Lists/LoadPeoplePicker/” is the method I want to call from my “Lists” controller.
- <script>
- $(document).ready(function () {
-
- PeoplePickerFor("structure_strGateKeeper1", "/api/Lists/LoadPeoplePicker/");
- });
- </script>
“PeoplePickerFor” method Code in the javascript file
Insert below code in a javascript file to have the PeoplePickerFor method which we called from the previous cshtml file. This method would call the controller method along with the search term.
-
-
-
- function PeoplePickerFor(TxtBox, SourceUrl) {
- $("#" + TxtBox).keyup(function (e) {
- if (e.keyCode == 8 || e.keyCode == 46) {
- var EmailAttrVal = $("#" + TxtBox).attr(TxtBox + "_email");
- if (EmailAttrVal) {
- $("#" + TxtBox).attr(TxtBox + "_email", "");
- $("#" + TxtBox).attr(TxtBox + "pp_resolved", false);
- }
- }
- });
-
- $("#" + TxtBox).autocomplete({
- source: function (request, response) {
- $.ajax({
- url: SourceUrl + request.term,
- dataType: "json",
- success: function (data) {
- console.log(data);
- var ErrorVal = $("#" + TxtBox + "pp_errordivid").text();
-
- if (data.length < 1) {
- if (ErrorVal == "" || ErrorVal == "undefined") {
- $("#" + TxtBox).after("<div style='color:red;font-weight:bold' id='" + TxtBox + "pp_errordivid'>No Valid Users Found</div>");
- }
- $("#" + TxtBox).attr(TxtBox + "pp_resolved", false);
- }
- else {
- $("#" + TxtBox + "pp_errordivid").remove();
- }
-
- response($.map(data, function (v, i) {
- return {
- label: v.displayName,
- value: v.displayName,
- text: v.email
- };
- }));
- }
- });
- },
- minLength: 2,
- select: function (event, ui) {
- if (!$.trim(ui.item.text)) {
- $("#" + TxtBox).attr(TxtBox + "_email", "");
- $("#" + TxtBox).attr(TxtBox + "pp_resolved", false);
- $("#" + TxtBox).after("<div style='color:red;font-weight:bold' id='" + TxtBox + "pp_errordivid'>Not a valid user</div>");
- }
- else {
- $("#" + TxtBox).attr(TxtBox + "_email", ui.item.text);
- $("#" + TxtBox).attr(TxtBox + "pp_resolved", true);
- }
- }
- });
- }
Controller Method calling Novell Directory so all users match search criteria
Insert the below code to enable the controller to access the interface that would further call the Novell Directory for accessing the LDAP. In my example, I am inserting the below code in the “Lists” controller.
- Private IAuthenticationService _authService;
-
- public ListsController(IAuthenticationService authService)
- {
- _authService = authService;
- }
In your controller, insert below code, so that below method can be called from the JavaScript above.
-
-
-
-
-
- [HttpGet("{searchText}")]
- [ActionName("LoadPeoplePicker")]
- public JsonResult LoadPeoplePicker([FromRoute] string searchText)
- {
- var UserList = new List<AppUser>();
- UserList = _authService.GetAllUserNames(searchText);
-
- return new JsonResult(UserList);
- }
Add the below code to a new class as we require a class to hold all properties to be fetched from LDAP.
- public class AppUser
- {
- public string DisplayName { get; set; }
- public string LoginName { get; set; }
- public List<string> Groups { get; set; } = new List<string>();
- public string Email { get; set; }
- public string LoginId { get; internal set; }
- public string FirstName { get; internal set; }
- public string LastName { get; internal set; }
- public string Title { get; internal set; }
- public string EmployeeId { get; internal set; }
- public string Department { get; internal set; }
- public string Division { get; internal set; }
- public string ManagerName { get; internal set; }
- public string ManagerId { get; internal set; }
- }
Method that connects to LDAP using Novell Directory
Create a new class and insert the below code to interface the class which would hold the method and details to connect the LDAP using Novell Directory.
- public interface IAuthenticationService
- {
- List<AppUser> GetAllUserNames(string SearchText);
- }
In the same class, insert the below code:
- public class LdapAuthenticationService : IAuthenticationService
- {
- private readonly LdapConnection _connection;
-
- public LdapAuthenticationService()
- {
- _connection = new LdapConnection
- {
- SecureSocketLayer = true
- };
- }
-
-
-
-
-
-
- public List<AppUser> GetAllUserNames(string SearchText)
- {
- _connection.Connect("adauth.corpads.local", LdapConnection.DEFAULT_SSL_PORT);
- _connection.Bind(User, password);
-
- var searchFilter = string.Format("(displayName={0}*)", SearchText);
- var result = _connection.Search(
- "CN=Users,dc=corpads,dc=local",
- LdapConnection.SCOPE_SUB,
- searchFilter,
- new[] { "sAMAccountName", "mail" },
- false
- );
-
- List<AppUser> UserNames = new List<AppUser>();
- while (result.hasMore())
- {
- var user = result.next();
- if (user != null)
- {
- user.getAttributeSet();
- AppUser UserDetails = new AppUser();
- UserDetails.LoginName = user.getAttribute("sAMAccountName").StringValue;
- UserDetails.Email = user.getAttribute("mail").StringValue;
- UserNames.Add(UserDetails);
- }
- }
- _connection.Disconnect();
- return UserNames;
- }
- }
Now execute the code and test the functionality by entering a few characters of your name in the control on the browser, then putting a debug point at the method in controller.
That's it. I hope you learned something new from this article and will utilize it in your work.