首页 新闻 赞助 找找看

AD域认证的问题

0
悬赏园豆:50 [待解决问题]

我写的系统需要对接公司AD域服务器做登录,

写的控制台项目测试

 static void Main(string[] args)
        {
            // string strLDAPFilter = string.Format(txtFilter.Text, txtUserName.Text.Trim());
            //deSearch.Filter = "(&(objectClass=user)(sAMAccountName=" + username + "))";  


            string TestUserID = "qimin";//"liuhao05";
            string TestUserPwd = "SJQsjq301301";
            string strLDAPFilter = String.Format("samAccountName={0}", TestUserID);
            LDAPHelper objldap = new LDAPHelper();
            string strLDAPPath = "LDAP://(域服务器IP):389";
            string strLDAPAdminName = "oa";
            string strLDAPAdminPwd = "WSXwsx2018!";
            string strMsg = "";
            bool blRet = objldap.OpenConnection(strLDAPPath, strLDAPAdminName, strLDAPAdminPwd);


            if (blRet)
            {
                blRet = objldap.CheckUidAndPwd(strLDAPFilter, TestUserID, TestUserPwd, ref strMsg);
                if (blRet)
                {
                    strMsg = "检测用户名" + TestUserID + "和密码" + TestUserPwd + "成功";
                }
                else if (!blRet && string.IsNullOrEmpty(strMsg))
                {
                    strMsg = "检测用户名" + TestUserID + "和密码" + TestUserPwd + "失败";
                }
            }
        }



帮助类
public class LDAPHelper
    {
        private DirectoryEntry _objDirectoryEntry;


        /// <summary>  
        /// 构造函数  
        /// </summary>  
        /// <param name="LADPath">ldap的地址,例如"LDAP://***.***.48.110:389/dc=***,dc=com"</param>  
        /// <param name="authUserName">连接用户名,例如"cn=root,dc=***,dc=com"</param>  
        /// <param name="authPWD">连接密码</param>  
        public bool OpenConnection(string LADPath, string authUserName, string authPWD)
        {    //创建一个连接   
            _objDirectoryEntry = new DirectoryEntry(LADPath, authUserName, authPWD, AuthenticationTypes.None);


            if (null == _objDirectoryEntry)
            {
                return false;
            }
            else if (_objDirectoryEntry.Properties != null && _objDirectoryEntry.Properties.Count > 0)
            {
                return true;
            }
            return false;
        }

     
        /// <summary>  
        /// 检测一个用户和密码是否正确  
        /// </summary>  
        /// <param name="strLDAPFilter">(|(uid= {0})(cn={0}))</param>  
        /// <param name="TestUserID">testuserid</param>  
        /// <param name="TestUserPwd">testuserpassword</param>  
        /// <param name="ErrorMessage"></param>  
        /// <returns></returns>  
        public bool CheckUidAndPwd(string strLDAPFilter, string TestUserID, string TestUserPwd, ref string ErrorMessage)
        {
            bool blRet = false;
            try
            {
                //创建一个检索  
                DirectorySearcher deSearch = new DirectorySearcher(_objDirectoryEntry);
                //过滤名称是否存在  
                deSearch.Filter = strLDAPFilter;
                deSearch.SearchScope = SearchScope.Subtree;


                //find the first instance   
                SearchResult objSearResult = deSearch.FindOne();


                //如果用户密码为空  
                if (string.IsNullOrEmpty(TestUserPwd))
                {
                    if (null != objSearResult && null != objSearResult.Properties && objSearResult.Properties.Count > 0)
                    {
                        blRet = true;
                    }
                }
                else if (null != objSearResult && !string.IsNullOrEmpty(objSearResult.Path))
                {
                    //获取用户名路径对应的用户uid  
                    int pos = objSearResult.Path.LastIndexOf('/');
                    string uid = objSearResult.Path.Remove(0, pos + 1);
                    DirectoryEntry objUserEntry = new DirectoryEntry(objSearResult.Path, TestUserID, TestUserPwd, AuthenticationTypes.Secure);
                    
                    if (null != objUserEntry && objUserEntry.Properties.Count > 0)
                    {
                        blRet = true;
                    }
                }
            }
            catch (Exception ex)
            {
                if (null != _objDirectoryEntry)
                {
                    _objDirectoryEntry.Close();
                }
                ErrorMessage = "检测异常:" + ex.StackTrace;
            }
            return blRet;
        }




        /// <summary>  
        /// 关闭连接  
        /// </summary>  
        public void closeConnection()
        {
            if (null != _objDirectoryEntry)
            {
                _objDirectoryEntry.Close();
            }
        }
    }

我根据用户输入的账号密码,用管理员账号抓出那个账号对应的path作为新的AD对象的path使用,后续用账号密码去验证。虽然我看到网上大多数例子说明第二个参数好像要写uid,但是我这里写了普通域账号可以抓出来自己的信息,但是碰到别人的就抓不出来了,直接报错,有人说我是第一个path写错了,我现在很迷惘

帅黑的主页 帅黑 | 初学一级 | 园豆:74
提问于:2018-03-13 18:26

建议给代码加上高亮

dudu 6年前
< >
分享
所有回答(1)
0

不写uid可能就默认使用当前连接用,建议加上uid,参考 [.NET Active Directory开发]根据NativeGuid获取DirectoryEntry实例

DirectoryEntry entry=new DirectoryEntry();
entry.Path = "LDAP://ServerAddress/<GUID=a46cc1f54eaf8c428cda3753e59265f6>";
entry.Username="Administrator";
entry.Password="Administrator_Password";
dudu | 园豆:31075 (高人七级) | 2018-03-13 21:08
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册