• <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

            最新評論

            閱讀排行榜

            亚洲精品国产成人99久久| 精品国产乱码久久久久软件| 狠狠色丁香久久婷婷综合五月| 亚洲精品国产字幕久久不卡| 国产精品毛片久久久久久久| 成人亚洲欧美久久久久| 久久久国产99久久国产一| 久久亚洲日韩精品一区二区三区| 免费国产99久久久香蕉| 久久综合九色欧美综合狠狠| 99国产欧美久久久精品蜜芽| 久久性精品| 日本精品久久久久中文字幕| 亚洲AV无码久久精品蜜桃| 国产一区二区精品久久岳| 久久99精品国产自在现线小黄鸭 | 久久久久人妻一区精品| 亚洲成色WWW久久网站| 精品99久久aaa一级毛片| 人妻精品久久久久中文字幕69 | 婷婷久久精品国产| www久久久天天com| 色诱久久久久综合网ywww| 久久亚洲国产成人影院| 国内精品久久久久久久coent| 久久人人妻人人爽人人爽| 无码国内精品久久综合88| 久久精品国产WWW456C0M| 国产福利电影一区二区三区久久久久成人精品综合 | 97久久国产综合精品女不卡| 久久伊人中文无码| 久久夜色撩人精品国产| 久久精品无码专区免费| 中文字幕亚洲综合久久2| 久久99国产精品99久久| 18岁日韩内射颜射午夜久久成人| 四虎国产精品成人免费久久 | 亚洲精品无码久久久久sm| 亚洲日韩欧美一区久久久久我| 久久av高潮av无码av喷吹| 久久精品国产一区二区三区|