如何判斷使用者是否為AD域群組成員

來源:互聯網
上載者:User
如何判斷使用者是否屬於某個域組中成員呢,查了很多資料.下面將找到的資料共用下.

UserisGroupMember(UserLogin, RoleName)   用來判斷使用者是否為域群組成員
注: 由於域組存在嵌套在其他域組的情況,我們需要進行遍曆操作.

   

Code
        private static string ADPath = "LDAP://domain";

        /**//// <summary>
        /// 判斷使用者是否為域群組成員
        /// </summary>
        /// <param name="UserLogin">使用者名稱</param>
        /// <param name="RoleName">域組名</param>
        /// <returns></returns>
        private static bool UserisGroupMember(string UserLogin, string RoleName)
        {
            DirectoryEntry entry = new DirectoryEntry(ADPath);
            DirectorySearcher mySearcher = new DirectorySearcher(entry); 

            mySearcher.Filter = string.Format("(&(objectClass=user)(sAMAccountName={0})) ", UserLogin);
            mySearcher.PropertiesToLoad.Add("memberof");
            SearchResult mysr = mySearcher.FindOne();            

            if (mysr.Properties.Count > 1)  // 返回兩個屬性,一個是內建的adspath,另一個是PropertiesToLoad載入的 
            {
                string[] memberof = new string[mysr.Properties["memberof"].Count];
                int i = 0;
                foreach (Object myColl in mysr.Properties["memberof"])
                {
                    memberof[i] = myColl.ToString().Substring(3, myColl.ToString().IndexOf(",") - 3);
                    if (memberof[i] == RoleName)
                       return true;
                    i++;
                }
                //其實這一層迴圈是廣度優先演算法,因為考慮到一個人直接屬於某個安全性群組的可能性要大一些,這樣做效率更高.如果把下面這個迴圈放到上面的if的esle中,就是完全的深度優先了. 
                foreach (string GroupName in memberof)
                {
                    if (MemberisGroupMember(GroupName, RoleName))
                        return true;
                }
            }
            return false;
        }

        private static bool MemberisGroupMember(string GroupName, string RoleName)
        {
            bool isfind = false;
            DirectoryEntry entry = new DirectoryEntry(ADPath);
            DirectorySearcher mySearcher = new DirectorySearcher(entry);
            mySearcher.Filter = string.Format("(&(objectClass=group)(CN={0})) ", GroupName);
            mySearcher.PropertiesToLoad.Add("memberof");
            SearchResult mysr = mySearcher.FindOne();
            string memberof;
            try
            {
                if (mysr.Properties.Count > 1) // 返回兩個屬性,一個是內建的adspath,另一個是PropertiesToLoad載入的 
                {
                    foreach (Object myColl in mysr.Properties["memberof"])
                    {
                        memberof = myColl.ToString().Substring(3, myColl.ToString().IndexOf(",") - 3);
                        if (memberof == RoleName)
                        { 
                            isfind = true;
                            break;
                        }
                        else if (MemberisGroupMember(memberof, RoleName))
                        { 
                            isfind = true;
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            { }
            return isfind;
        }      

參考資料:
http://www.cnblogs.com/zyk/archive/2004/11/02/59707.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.