• 网络学院
  • 新手学堂
  • 操作系统
  • 网络技术
  • 软件应用
  • 办公软件
  • 编程技术
  • 网站架设
  • 数据库类
  • 平面设计
  • 多媒体类
  • 游戏资讯
  • 教学论文
  • 认证考试
C#2.0
广告位
  站点:
  • 首 页
  • 最新软件
  • 最新文章
  • 国内软件
  • 国外软件
  • 汉化软件
  • 源码下载
  • 字体下载
C#2.0 对AD的简单操作
软件发布 C#2.0 对AD的简单操作
网络软件 系统工具 应用软件 联络聊天 图形图像 多媒体类 行业软件 游戏娱乐 编程开发 安全相关 教育教学 数码软件
热门软件: QQ 瑞星 pplive e话通 木马克星 千千静听 office2000 五笔字根 Photoshop 视频分割
返回首页 | 文章首页 | 编程技术 | C#教程 | C#2.0 对AD的简单操作

C#2.0 对AD的简单操作

 

添加时间: 2007-7-15 4:05:51  作者: C#教程  阅读次数:111   来源: http://www.d9soft.com

 

 

       

    System.DirectoryServices.Protocols.dll是.net2.0新增加的一个针对目录服务访问协议处理的组件,其下只有一个System.DirectoryServices.Protocols命名空间。在该命名空间下,主要有LDAP、DSML两种国际标准协议的一系列实现类。通过这些类,完全可以很方便地实现对目录的操作管理,这个实现步骤就有点类似你利用ADO.NET操作数据库一样方便。

    在System.DirectoryServices.Protocols命名空间里,主要有这样几个类:LdapConnection(LDAP协议方式的目录连接类,负责创建LDAP连接并绑定LDAP服务器)、DsmlSoapHttpConnection(DSML协议方式的目录连接类、负责创建DSML连接并绑定DSML服务器)、AddRequest/AddResponse、ModifyRequest/ModifyResponse、ModifyDNRequest/ModifyDNResponse、CompareRequest/CompareResponse、SearchRequest/SearchResponse、DeleteRequest/DeleteResponse、DsmlRequestDocument/DsmlResponseDocument。这些类在实际编程应用中的关系如下图:

    用户利用LdapConnection/DsmlSoapHttpConnection跟LDAP服务器/DSML服务器建立连接并绑定后,即可创建一系列相应的操作请求(如增加一新对象请求AddRequest),然后通过连接对象的SendRequest方法把请求命令发送到服务器,服务器根据请求进行相应处理后,把应答信息传回给客户端。需要指出的是,对于DSML方式的请求,还可以利用DsmlRequestDocument将AddRequest、ModifyRequest、ModifyDNRequest、CompareRequet、SearchRequest和DeleteRequest的任意几个请求组合组装起来,一并发送到DSML服务器进行处理。

        LdapConnection的使用:

创建LDAP连接并进行绑定:

NetworkCredential credential = new NetworkCredential("Administrator", "password");XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" />

 LdapConnection ldapConnection = new LdapConnection("192.168.0.6");

ldapConnection.Credential = credential;

ldapConnection.Bind();

创建一个请求,使其达到增加一个OU,其名称为MyOU的目的。创建后的MyOU其DN为OU=MyOU,DC=mydomain,DC=local:

string targetDN = "DC=mydomain,DC=local"; 

// 增加一个名为MyOU的组织单元

string ou = "OU=MyOU," + targetDN;

string objectClass = "organizationalUnit"; 

AddRequest addRequest = new AddRequest(ou, objectClass);

把请求发送到服务器进行处理:

ldapConnection.SendRequest(addRequest);
执行完SendRequest()后,如果没有出现异常,那么MyOU已经成功增加了。当然,如果你还需要进一步对SendRequest()操作后的应答信息进行处理的话,也可以类似下面这样写,其中将在屏幕上输出“Success“的结果码:

AddResponse addResponse = (AddResponse)ldapConnection.SendRequest(addRequest); 

Console.WriteLine(addResponse.ResultCode.ToString());


        至此,一个LDAP请求已经处理完毕。上面的完整代码可以点这里进行查看。

using System;

using System.Net;

using System.DirectoryServices;

using System.DirectoryServices.Protocols; 

NetworkCredential credential = new NetworkCredential("Administrator", "password"); 

LdapConnection ldapConnection = new LdapConnection("192.168.0.6");

ldapConnection.Credential = credential;

ldapConnection.Bind(); 

string targetDN = "DC=mydomain,DC=local"; 

//增加一个名为MyOU的OU

string ou = "OU=MyOU," + targetDN;

string objectClass = "organizationalUnit"; 

AddRequest addRequest = new AddRequest(ou, objectClass);

AddResponse addResponse = (AddResponse)ldapConnection.SendRequest(addRequest); 

Console.WriteLine(addResponse.ResultCode.ToString()); 


        类似上面增加操作,还可以利用DeleteRequest进行删除操作、ModifyDNRequest进行重命名或移动操作、ModifyRequest进行修改对象属性操作、SearchRequest进行查询操作、CompareRequest进行验证比较操作。

        DsmlSoapHttpConnection的使用:

关于DSML for Windows,可以通过http://www.microsoft.com/windows2000/server/evaluation/news/bulletins/dsml.asp进行下载。简单理解DSML,就是利用标准的HTTP/SOAP/XML对活动目录进行读写等一系列操作的技术。安装完DSML for Windows后,还需要执行“Microsoft DSML“程序组里的“Configuring DSML Services“。该程序里有三个步骤,很好理解和操作,这里省略。但需要指出,如果你不需要进行SSL连接服务器的话,需要在Step 1中把“Require SSL to connect to DSML server“取消。另外,如果你还需要执行除读取以外的权限,如写操作权限,还需要在Step 2中把“Make DSML Server readonly“取消。
建立DsmlSoapHttpConnection连接:

NetworkCredential credential = new NetworkCredential("Administrator", "password"); 

Uri dsmlServerUri = new Uri("http://192.168.0.6/dsml/adssoap.dsmlx");

DsmlSoapHttpConnection dsmlSoapHttpConnection = new DsmlSoapHttpConnection(dsmlServerUri);

dsmlSoapHttpConnection.Credential = credential;

创建一个请求,使其达到增加一个OU,其名称为MyOU的目的。创建后的MyOU其DN为OU=MyOU,DC=mydomain,DC=local。可以看到,这部分跟LDAP操作时一致的!

string targetDN = "DC=mydomain,DC=local"; 

//增加一个名为MyOU的OU

string ou = "OU=MyOU," + targetDN;

string objectClass = "organizationalUnit"; 

AddRequest addRequest = new AddRequest(ou, objectClass);

把请求发送到服务器进行处理:

DsmlResponseDocument dsmlResponseDocument = dsmlSoapHttpConnection.SendRequest(addRequest); 

Console.WriteLine(dsmlResponseDocument[0].ResultCode.ToString());
可以看到,DsmlResponseDocument可以包含多个应答信息,应该还需要指定下标,才能得到具体返回的结果码。

      至此,一个DSML请求已经处理完毕。上面的完整代码可以点这里进行查看。

using System;

using System.Net;

using System.DirectoryServices;

using System.DirectoryServices.Protocols;

 NetworkCredential credential = new NetworkCredential("Administrator", "password"); 

Uri dsmlServerUri = new Uri("http://192.168.0.6/dsml/adssoap.dsmlx");

DsmlSoapHttpConnection dsmlSoapHttpConnection = new DsmlSoapHttpConnection(dsmlServerUri);

dsmlSoapHttpConnection.Credential = credential; 

string targetDN = "DC=mydomain,DC=local"; 

//增加一个名为MyOU的OU

string ou = "OU=MyOU," + targetDN;

string objectClass = "organizationalUnit"; 

AddRequest addRequest = new AddRequest(ou, objectClass);

DsmlResponseDocument dsmlResponseDocument = dsmlSoapHttpConnection.SendRequest(addRequest);

 Console.WriteLine(dsmlResponseDocument[0].ResultCode.ToString()); 


        另外,对于DSML的多条操作请求一起发送的情况,可以创建DsmlRequestDocument对象去包含各种操作请求,具体可以参考这里的代码。

using System;

using System.Net;

using System.DirectoryServices;

using System.DirectoryServices.Protocols; 

NetworkCredential credential = new NetworkCredential("Administrator", "password"); 

ri dsmlServerUri = new Uri("http://192.168.0.6/dsml/adssoap.dsmlx");

DsmlSoapHttpConnection dsmlSoapHttpConnection = new DsmlSoapHttpConnection(dsmlServerUri);

dsmlSoapHttpConnection.Credential = credential; 

string targetDN = "DC=mydomain,DC=local"; 

string ou = "OU=MyOU," + targetDN;

string objectClass = "organizationalUnit"; 

DsmlRequestDocument batchRequest = new DsmlRequestDocument(); 

AddRequest addRequest;

ModifyRequest modifyRequest; 

addRequest = new AddRequest(ou, objectClass);

addRequest.RequestId = "Add1";

batchRequest.Add(addRequest); 

modifyRequest = new ModifyRequest(ou, "description", new string[]{"This is description of MyOU"}, DirectoryAttributeOperation.Replace);

modifyRequest.RequestId = "Modify1";

batchRequest.Add(modifyRequest); 

DsmlResponseDocument batchResponse = dsmlSoapHttpConnection.SendRequest(batchRequest);

foreach (DirectoryResponse response in batchResponse)

{

      Console.WriteLine(response.GetType().Name + ": \tId=" + response.RequestId + ",\tResultCode=" + response.ResultCode);

}
 
     感觉怎样?我想肯定舒服了很多,毕竟这样的编程逻辑对我们来说,是再熟悉也不错的了。另外,对于目录这块,.NET2.0还在System.DirectoryServices.dll组件里也增加了一个新的命名空间System.DirectoryServices.ActiveDirectory。顾名思义,该命名空间完成的功能就是对活动目录进行更完整的处理操作,比如对域林、域树、域、域控制器、目录复制、活动目录架构、域信任等的操作,让你尽可能完全在纯托管的代码中实现对活动目录的操作。

 

 

 

 

上下文章:

 

上一篇文章: 活动目录.NET编程Tips 下一篇文章: Raw Socket编程实现网络封包监视

相关文章:

  • Photoshop简单的艺术化效果
  • 在ORACLE的存储过程中如何做简单的动态查询
  • Oracle数据操作和控制语言详解
  • Oracle操作经验谈
  • Oracle数据操作和控制语言详解

相关软件:

  • TTVNC 最简单的远程协助软件 V2.1
  • 简单五笔 V1.7
  • 简单游绿色梦幻游戏工具平台 V3.70.2181
  • 简单进销存管理软件 V2.30
  • 操作系统 -- 安装过程详细图解 一步步教你(图片)从98版到Linux
  • 简单影音 V2008 01.25

 

 

快速导航

  • 网络学院
  • 精品汇聚
  • 字体下载
  • 教程下载
  • ASP源码
  • PHP源码
  • Net源码
  • JSP 源码

编程技术分类导航

  • ASP & ASP.NET教程
  • PHP教程
  • JSP教程
  • C/C++教程
  • VB & VB.NET教程
  • VC教程
  • Delphi教程
  • BCB教程
  • VFP教程
  • PB教程
  • JAVA教程
  • XML教程
  • C#教程
  • CGI教程

本类经典文章推荐

  • ADO.NET的开发场景及传统ADO的处理
  • 利用Visual C#实现Window管道技术
  • C#取得汉字的拼音的首字母
  • 使用C#编写DES加密程序的framework
  • Visual C#编写3D游戏框架示例
  • 用C#和本地Windows API操纵系统菜...
  • 在C#程序设计中使用Win32类库
  • Visual C#中调用Windows服务初探
  • 如何在C#的WinForm中制作饼状图和...
  • C#中实现DataGrid双向排序

C#教程阅读排行

  • 如何在C#的WinForm中制作饼状图和...
  • 浅析C#中图形编程
  • 用C#和本地Windows API操纵系统菜...
  • 彻底剖析C# 2.0泛型类的创建和使用
  • Visual C#编写3D游戏框架示例
  • 使用C#编写DES加密程序的framework
  • Visual C# 2005实现控件中捕获按键
  • Visual C#中调用Windows服务初探
  • C#如何在Form中嵌入并且操作Excel...
  • C#中利用mediaplayer打造mp3播放器

编程技术阅读总排行

  • VB入门教程之一
  • 第二章 PowerBuilder 入门之创建新...
  • Java连接数据库实例
  • 第一章 什么是PowerBuilder
  • VC++之List Box/Check List Box控...
  • VC++ List Ctrl控件
  • VC++ Combo Box/Combo Box Ex控件
  • 学C++不得不看的一篇文章
  • VB入门教程之二
  • VC++之Button控件

广告位置

字母检索 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 回到顶部

关于我们 | 版权声明 | 免责条款 | 广告联系 | 软件发布 | 下载帮助 | 下载排行 | 网站地图 | 特别鸣谢 | 友情连接

copyright; 2005-2008 D9soft.com 第九软件网 版权所有