• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++ Programmer's Cookbook

            {C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

            Active Directory如何用C#進行增加、刪除、修改、查詢用戶與組織單位!

            ?

            ?

            Active Directory如何用C#進行增加、刪除、修改、查詢用戶與組織單位!

            首先我們來了解一下什么是Active Directory。不用我描述,看以下網(wǎng)址,或在.net自帶幫助文檔里根據(jù)Active Directory關鍵字一搜,就什么都明白了。
            http://developer.ccidnet.com/pub/article/c322_a28703_p2.html

            接下來,我們來看看權限。你可以通過“網(wǎng)上鄰居--整個網(wǎng)絡--Directory--demain(你的域名)”你就可以看到所有關于域下的信息,粗一看就知道是怎么回事了。
            需要告訴大家的:所有組織單位下的用戶都在Users(容器)--Demain Users(組)中
            用代碼進行訪問時,如果你是域管理員用戶,則可以做任何操作,否則,只能查詢用戶屬性。

            private void SearchUser()
            {
            string domainName = "Domain";
            string groupName = "Domain Users";
            string dirmemName="";
            //在Domain Users域用戶里取得每個用戶名
            System.DirectoryServices.DirectoryEntry group = new System.DirectoryServices.DirectoryEntry("WinNT://" + domainName + "/" + groupName + ",group");
            foreach(Object member in (IEnumerable)group.Invoke("Members"))
            {
            //根據(jù)很個用戶生成如:"LDAP://OU=套裝軟體課,OU=系統(tǒng)開發(fā)部,OU=資訊服務處,OU=營運支援中心,OU=XX公司,DC=Domain,DC=com,DC=cn"
            System.DirectoryServices.DirectoryEntry dirmem = new System.DirectoryServices.DirectoryEntry(member);
            dirmemName=dirmem.Name;
            string DomainName="Domain";
            string FilterStr = "(sAMAccountname="+dirmemName+")";
            System.DirectoryServices.DirectorySearcher FindMe = new System.DirectoryServices.DirectorySearcher(DomainName);
            FindMe.Filter = FilterStr;
            System.DirectoryServices.SearchResult FindRes = FindMe.FindOne();
            System.DirectoryServices.DirectoryEntry MyUser = FindRes.GetDirectoryEntry();
            string OUPath=MyUser.Parent.Path;
            //找到該用戶所在的LDAP:后,由域管理員登錄,并取得該用戶的所在屬性。
            string strFieldsValue="",strFields="";
            System.DirectoryServices.DirectoryEntry myds=new System.DirectoryServices.DirectoryEntry(OUPath,"域管理員名","域管理員密碼");
            foreach(System.DirectoryServices.DirectoryEntry tempEntry in myds.Children)
            {
            if(tempEntry.SchemaClassName.ToString() == "user" && tempEntry.Properties["sAMAccountName"].Value.ToString().ToLower()==dirmemName)
            {
            foreach (string propertyName in tempEntry.Properties.PropertyNames )
            {
            string oneNode = propertyName + ": " +
            entry.Properties[propertyName][0].ToString();
            this.Textbox1.Text=oneNode;
            }
            }







            public void AddUser(string strPath,string Username,string ChineseName)//strPath 增加用戶至哪個組織單位如"LDAP://OU=XX公司,DC=Domain,DC=com"帳號、中文名{
            try
            {
            string RootDSE;
            //System.DirectoryServices.DirectorySearcher DSESearcher= new System.DirectoryServices.DirectorySearcher();
            //RootDSE=DSESearcher.SearchRoot.Path;
            //RootDSE="LDAP://DC=Domain,DC=com";
            //RootDSE=RootDSE.Insert(7,"CN=Users,");
            System.DirectoryServices.DirectoryEntry myDE = new System.DirectoryServices.DirectoryEntry(strPath);
            System.DirectoryServices.DirectoryEntries myEntries = myDE.Children;
            // Create a new entry 'Sample' in the container.
            string strname="CN="+ChineseName;
            System.DirectoryServices.DirectoryEntry myDirectoryEntry = myEntries.Add(strname, "user");

            //MessageBox.Show(myDirectoryEntry.SchemaClassName.ToString());
            myDirectoryEntry.Properties["userPrincipalName"].Value=Username;
            myDirectoryEntry.Properties["name"].Value=ChineseName;
            myDirectoryEntry.Properties["samAccountName"].Value=Username;
            myDirectoryEntry.Properties["userAccountControl"].Value =66048; //590336;
            myDirectoryEntry.CommitChanges();
            }



            private void addOU(string strPath,string OUName)//增加組織到strPath組織單位下,組織名稱
            {
            try
            {
            //String RootDSE;
            //System.DirectoryServices.DirectorySearcher DSESearcher= new System.DirectoryServices.DirectorySearcher();
            //RootDSE=DSESearcher.SearchRoot.Path;
            //RootDSE="LDAP://OU=百意時尚廣場,DC=Domain,DC=com";

            System.DirectoryServices.DirectoryEntry myDE = new System.DirectoryServices.DirectoryEntry(strPath);
            System.DirectoryServices.DirectoryEntries myEntries = myDE.Children;
            string name="OU="+OUName;
            System.DirectoryServices.DirectoryEntry myDirectoryEntry = myEntries.Add(name,"organizationalUnit");

            myDirectoryEntry.Properties["name"].Value=OUName;
            myDirectoryEntry.Properties["instanceType"].Value=4;
            myDirectoryEntry.Properties["distinguishedName"].Value="OU="+OUName+",DC=Domain,DC=COM)";
            myDirectoryEntry.Properties["objectCategory"].Value="CN=Organizational-Unit,CN=Schema,CN=Configuration,DC=sedep,DC=COM";
            myDirectoryEntry.Properties["ou"].Value=OUName;
            myDirectoryEntry.Properties["postalCode"].Value="777";

            myDirectoryEntry.CommitChanges();
            //UserMoveto("LDAP://OU="+OUName+",DC=sedep,DC=com",strPath);
            }
            catch(Exception RaiseErr)
            {
            MessageBox.Show (RaiseErr.Message);
            }
            }



            private void ModifyUser()
            {
            try
            {
            string DomainName="Domain";
            string FilterStr = "(sAMAccountname=karlluo)";
            System.DirectoryServices.DirectorySearcher FindMe = new System.DirectoryServices.DirectorySearcher(DomainName);
            FindMe.Filter = FilterStr;
            System.DirectoryServices.SearchResult FindRes = FindMe.FindOne();
            string tt=FindRes.Path;
            System.DirectoryServices.DirectoryEntry MyUser = FindRes.GetDirectoryEntry();
            string OUPath=MyUser.Parent.Path;

            DirectoryEntry myds=new DirectoryEntry(OUPath,"域管理員名","域管理員密碼");

            foreach(System.DirectoryServices.DirectoryEntry tempEntry in myds.Children)
            {
            if(tempEntry.SchemaClassName.ToString() == "user")
            {
            if(tempEntry.Properties["sAMAccountName"].Value.ToString().ToLower()=="karlluo")
            {
            tempEntry.UsePropertyCache=true;
            tempEntry.Properties["st"].Value="yyyyyyyyyyyyyyyy";
            //newEntry.Properties["userPrincipalName"].Value="userID";
            tempEntry.CommitChanges();
            }
            }
            }
            }
            catch(Exception RaiseErr)
            {
            MessageBox.Show (RaiseErr.Message);
            }

            }


            ?
            Copyright ? 2006 www.urok.cn AND shop33266099.taobao.com, All Rights Reserved.

            posted on 2006-07-24 09:06 夢在天涯 閱讀(1612) 評論(0)  編輯 收藏 引用 所屬分類: C#/.NET

            公告

            EMail:itech001#126.com

            導航

            統(tǒng)計

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804603
            • 排名 - 5

            最新評論

            閱讀排行榜

            国产精品亚洲综合专区片高清久久久 | 久久亚洲精品无码AV红樱桃| 精品久久综合1区2区3区激情 | 亚洲国产精品成人久久蜜臀| 99久久国产精品免费一区二区| 久久人人爽人人爽人人片av麻烦 | 人人狠狠综合久久88成人| 久久久精品人妻一区二区三区蜜桃 | 久久强奷乱码老熟女| 久久久国产精品亚洲一区| 久久97精品久久久久久久不卡 | 亚洲伊人久久综合影院| 久久精品亚洲精品国产色婷 | 欧美一区二区三区久久综合 | 久久香蕉国产线看观看乱码| 超级碰碰碰碰97久久久久| www性久久久com| 亚洲精品无码久久久影院相关影片| 久久91精品国产91久| 久久国产亚洲精品| 亚洲日本久久久午夜精品| 亚洲国产香蕉人人爽成AV片久久| 国产69精品久久久久9999| 久久久久久毛片免费看| 久久受www免费人成_看片中文| 日本欧美国产精品第一页久久| 国产成人综合久久综合| 欧美久久综合九色综合| 日本精品久久久久久久久免费| 久久国产亚洲高清观看| 国产A三级久久精品| 伊人久久成人成综合网222| 色综合久久中文综合网| 久久国产一区二区| 国产精品视频久久久| 国产精品美女久久久久| 77777亚洲午夜久久多喷| 久久午夜伦鲁片免费无码| 浪潮AV色综合久久天堂| 99久久免费国产特黄| 久久国产一区二区|