csharp 验证LDAP服务器中的用户

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 验证LDAP服务器中的用户相关的知识,希望对你有一定的参考价值。

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

以上是关于csharp 验证LDAP服务器中的用户的主要内容,如果未能解决你的问题,请参考以下文章