• <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。不用我描述,看以下網址,或在.net自帶幫助文檔里根據Active Directory關鍵字一搜,就什么都明白了。
            http://developer.ccidnet.com/pub/article/c322_a28703_p2.html

            接下來,我們來看看權限。你可以通過“網上鄰居--整個網絡--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"))
            {
            //根據很個用戶生成如:"LDAP://OU=套裝軟體課,OU=系統開發部,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 夢在天涯 閱讀(1617) 評論(0)  編輯 收藏 引用 所屬分類: C#/.NET

            公告

            EMail:itech001#126.com

            導航

            統計

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

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1807723
            • 排名 - 5

            最新評論

            閱讀排行榜

            91精品免费久久久久久久久| 蜜桃麻豆www久久国产精品| 色偷偷88欧美精品久久久 | 免费久久人人爽人人爽av| 亚洲国产精品久久| 久久强奷乱码老熟女| 欧美日韩精品久久久久| 久久亚洲欧美国产精品| 精品久久久久中文字幕一区| 无码人妻久久一区二区三区蜜桃| www久久久天天com| 国产精品久久久久久久人人看| 久久91亚洲人成电影网站| 久久亚洲中文字幕精品一区| 亚洲综合久久综合激情久久| 狠狠色噜噜色狠狠狠综合久久| 91麻豆精品国产91久久久久久| 亚洲国产另类久久久精品小说 | 久久国产一区二区| 久久强奷乱码老熟女网站| 国产精品九九九久久九九| 久久国产劲爆AV内射—百度| 狠狠综合久久综合中文88| 2021少妇久久久久久久久久| 久久人妻AV中文字幕| 欧美精品九九99久久在观看| 狠狠综合久久综合中文88| 91精品国产91久久久久久| 国产精品久久久久久久久鸭| 日韩人妻无码精品久久久不卡| 偷偷做久久久久网站| 欧美粉嫩小泬久久久久久久| 久久精品国产欧美日韩| 精品久久久久久久久久久久久久久| 国产精品久久久久久久久| 国产精品无码久久综合| 久久99精品久久只有精品| 久久久亚洲欧洲日产国码aⅴ| 精品久久无码中文字幕| 久久91精品国产91久久麻豆| 中文字幕成人精品久久不卡|