Wednesday 28 July 2021

Nomenclature Rules

Capitalization Rules

  1. The PascalCasing convention, used for all identifiers except parameter names, capitalizes the first character of each word (including acronyms over two letters in length), as shown in the following examples:
                PropertyDescriptor HtmlTag

                DO use PascalCasing for

  • Namespace
  • Type
  • Interface
  • Method
  • Property
  • Event
  • Field
  • Enum value

  1.  The camelCasing convention, used only for parameter names, capitalizes the first character of each word except the first word, as shown in the following examples. As the example also shows, two-letter acronyms that begin a camel-cased identifier are both lowercase.

        propertyDescriptor ioStream htmlTag

        DO use camelCasing for parameter names.

Case Sensitivity

DO NOT assume that all programming languages are case sensitive. They are not. Names cannot differ by case alone.

Word Choice

  1. DO choose easily readable identifier names.
    For example, a property named HorizontalAlignment is more English-readable than AlignmentHorizontal.
  2.  DO NOT use underscores, hyphens, or any other nonalphanumeric characters.
  3. DO NOT use Hungarian notation.
  4.  AVOID using identifiers that conflict with keywords of widely used programming languages.


 Using Abbreviations and acronyms

  1. DO NOT use abbreviations or contractions as part of identifier names.
    For example, use GetWindow rather than GetWin.
  2. X DO NOT use any acronyms that are not widely accepted, and even if they are, only when necessary.

Avoid Language Specific Names

  1. DO use semantically interesting names rather than language-specific keywords for type names.
    For example, GetLength is a better name than GetInt.
  2. DO use a generic CLR type name, rather than a language-specific name, in the rare cases when an identifier has no semantic meaning beyond its type.
Names of Classes, Structs, and Interfaces
  1. DO name classes and structs with nouns or noun phrases, using PascalCasing.
  2. DO name interfaces with adjective phrases, or occasionally with nouns or noun phrases.
  3. DO NOT give class names a prefix (e.g., "C").
  4. CONSIDER ending the name of derived classes with the name of the base class.
  5. DO prefix interface names with the letter I, to indicate that the type is an interface.
  6. DO ensure that the names differ only by the "I" prefix on the interface name when you are defining a class–interface pair where the class is a standard implementation of the interface.

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;
            }
        }