hemtros
3/21/2017 - 9:07 PM

Verifying user in LDAP Server

Verifying user in LDAP Server

public static bool IsAuthenticated(string username, string pwd)
        {
            try
            {
                string domain = ContentValue.GetByParameterName("ActiveDirectoryDomain", 0, "en").Value.ToString();
                SendEventLogForDebugging("System Script", "IsAuthenticated", "Domain: " + domain);
                string path = ConfigurationManager.ConnectionStrings["LdapAuthenticationPath"].ConnectionString;
                SendEventLogForDebugging("System Script", "IsAuthenticated", "Path: " + path);
                String domainAndUsername = domain + @"\" + username;
                SendEventLogForDebugging("System Script", "IsAuthenticated", "domainAndUsername: " + domainAndUsername);
                DirectoryEntry entry = new DirectoryEntry(path, domainAndUsername, pwd);
                //DirectoryEntry entry = new DirectoryEntry(path, domainAndUsername, pwd, AuthenticationTypes.SecureSocketsLayer);

                //Bind to the native AdsObject to force authentication. 
                //Object obj = entry.NativeObject;


                DirectorySearcher search = new DirectorySearcher(entry);

                search.Filter = "(SAMAccountName=" + username + ")";
                search.PropertiesToLoad.Add("cn");
                SearchResult result = search.FindOne();

                if (null == result)
                {
                    SendEventLogForDebugging("System Script", "IsAuthenticated", "Username and password is Not authenticate");
                    return false;
                }
                else
                {
                    SendEventLogForDebugging("System Script", "IsAuthenticated", "Username and password is authenticate");
                    return true;
                }
            }
            catch (Exception ex)
            {
                SendEventLog(EventLogEntryType.Error.ToString(), "SystemScript - IsAuthenticated", ex.ToString());
                SendEventLogForDebugging("System Script", "IsAuthenticated", ex.Message);
                return false;
            }
        }