Monday, 19 July 2021

LDAP integration

LDAP stands for Lightweight Directory Access Protocol, and allows usage of single user account directory to login to various applications. LDAP integration allows your knowledge base instance to use your existing LDAP server as the master source of user data. 

C# Code


[Route("LDAP/{UserName}/{Password}")]
        [HttpGet]
        public HttpResponseMessage LDAPDetails(string UserName = "", string Password = "")
        {
            try
            {
                var jsonString = "";
                string adServer = ConfigurationManager.AppSettings["Server"].ToString();  //"192.168.5.10";
                string adDomain = ConfigurationManager.AppSettings["Domain"].ToString(); //"headoffice.ashishsrivastava.co.in";
                string adUsername = UserName;
                string password = Password;
                string[] dc = adDomain.Split('.');
                string dcAdDomain = string.Empty;

                foreach (string item in dc)
                {
                    if (dc[dc.Length - 1].Equals(item))
                        dcAdDomain = dcAdDomain + "DC=" + item;
                    else
                        dcAdDomain = dcAdDomain + "DC=" + item + ",";
                }

                //DirectoryEntry de = new DirectoryEntry("LDAP://" + adServer + "/CN=Users," + dcAdDomain, adUsername, password);
                DirectoryEntry de = new DirectoryEntry("LDAP://" + adServer + dcAdDomain, adUsername, password);

                DirectorySearcher ds = new DirectorySearcher(de);

                ds.SearchScope = SearchScope.Subtree;

                //ds.Filter = "(&(objectClass=User)(sAMAccountName=" + UserName + "))";
                ds.Filter = "(&(objectClass=User))";

   if (ds.FindOne() != null)
   {
   jsonString = "{ "
   + JsonConvert.SerializeObject("code", Formatting.Indented) + ":" + JsonConvert.SerializeObject("-200", Formatting.Indented)
                                                   + ","
                           + JsonConvert.SerializeObject("Response", Formatting.Indented) + ":" + JsonConvert.SerializeObject("User Login Succesfully!", Formatting.Indented) + " }";
                    var response = Request.CreateResponse(HttpStatusCode.OK);
                    response.Content = new StringContent(jsonString, Encoding.UTF8, "application/json");
                    return response;
                }
                else
                {
                    jsonString = "{ "
                                 + JsonConvert.SerializeObject("code", Formatting.Indented) + ":" + JsonConvert.SerializeObject("-403", Formatting.Indented)
                                                      + ","
                              + JsonConvert.SerializeObject("Response", Formatting.Indented) + ":" + JsonConvert.SerializeObject("The user name or password is incorrect!" + ds.ToString(), Formatting.Indented) + " }";
                    var response = Request.CreateResponse(HttpStatusCode.OK);
                    response.Content = new StringContent(jsonString, Encoding.UTF8, "application/json");
                    return response;
                }
            }
            catch (Exception ex)
            {
                string jsonString;
                if (ex.ToString().Contains("The user name or password is incorrect"))
                {
                    jsonString = "{ "
                                + JsonConvert.SerializeObject("code", Formatting.Indented) + ":" + JsonConvert.SerializeObject("-403", Formatting.Indented)
                                                     + ","
                             + JsonConvert.SerializeObject("Response", Formatting.Indented) + ":" + JsonConvert.SerializeObject("The user name or password is incorrect", Formatting.Indented) + " }";
                }
                else
                {
                    jsonString = "{ "
                                + JsonConvert.SerializeObject("code", Formatting.Indented) + ":" + JsonConvert.SerializeObject("-500", Formatting.Indented)
                                                     + ","
                             + JsonConvert.SerializeObject("Response", Formatting.Indented) + ":" + JsonConvert.SerializeObject(ex.ToString(), Formatting.Indented) + " }";
                }

                var response = Request.CreateResponse(HttpStatusCode.OK);
                response.Content = new StringContent(jsonString, Encoding.UTF8, "application/json");
                return response;
            }
        }

No comments:

Post a Comment

Upload valid file in C#

    protected bool CheckFileExtandLength(HttpPostedFile HtmlDocFile)     {         try         {             Dictionary<string, byte[]...