• <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 夢在天涯 閱讀(1618) 評論(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

            搜索

            •  

            積分與排名

            • 積分 - 1808261
            • 排名 - 5

            最新評論

            閱讀排行榜

            色婷婷久久综合中文久久一本| 无码人妻久久一区二区三区蜜桃| 欧洲国产伦久久久久久久 | 亚洲欧美另类日本久久国产真实乱对白| 久久伊人影视| 中文字幕人妻色偷偷久久| 91精品国产高清久久久久久io| 精品久久人人妻人人做精品| 久久精品99久久香蕉国产色戒| 久久精品中文字幕第23页| 2021少妇久久久久久久久久| 精品乱码久久久久久夜夜嗨| 性欧美丰满熟妇XXXX性久久久| 中文字幕亚洲综合久久菠萝蜜| 国产亚洲精久久久久久无码| 色婷婷综合久久久久中文 | 久久精品国产影库免费看| 久久亚洲熟女cc98cm| 久久精品无码一区二区app| 日韩精品久久无码中文字幕 | 久久久久人妻一区二区三区| 国产日韩久久免费影院| jizzjizz国产精品久久| 一本大道久久东京热无码AV| 色综合合久久天天综合绕视看| 狠狠色婷婷综合天天久久丁香| 久久婷婷五月综合97色直播| 久久久噜噜噜久久| 亚洲伊人久久大香线蕉苏妲己| 2020最新久久久视精品爱| 麻豆AV一区二区三区久久| 久久AV无码精品人妻糸列| 国产免费久久精品99re丫y| 久久综合精品国产一区二区三区| 岛国搬运www久久| 国产精品免费久久| AA级片免费看视频久久| 97久久精品人人澡人人爽| 99久久国产亚洲高清观看2024 | 69SEX久久精品国产麻豆| 91久久婷婷国产综合精品青草|