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。顾名思义,该命名空间完成的功能就是对活动目录进行更完整的处理操作,比如对域林、域树、域、域控制器、目录复制、活动目录架构、域信任等的操作,让你尽可能完全在纯托管的代码中实现对活动目录的操作。
分享到:
相关推荐
8.1 AD0.NET的基本对象及其操作 8.1.1 Connection对象(连接数据库) 8.1.2 Command对象 8.1.3 DataReader对象 8.1.4 DataAdapter和DataSet对象 8.1.5 使用DataView过滤数据 8.1.6 动态创建DataTable对象 8.2...
ldap c# 测试工具以及源代码(vs2010 winform .net2.0) 里面可以测试各种连接,主要是用来验证用户和密码是否正确
对于调用京东商城的API接口有很大帮助,省时又省力。轻松实现调用。
本书介绍了如何用ADO.NET 2.0对独立应用、企业级应用和Web应用程序中的数据进行访问、排序和操作。作者针对如何利用Visual Studio 2005中的新工具和向导,编写、测试并调试数据库应用程序代码,用丰富的示例代码、...
独立自足的便携式RF学习模块 高性价比实验平台 基于ADI公司的AD9363--高度集成的RF捷变收发器和Xilinx® Zynq Z-7010 FPGA RF频率范围:325 MHz至3.8 ...USB 2.0供电接口,带有Micro-USB 2.0连接器 高质量塑料外壳
2.ASP.NET.2.0.高级编程(第4版) [1/7] 原书名: Professional ASP.NET 2.0 原出版社: Wrox 作者:(美)Bill Evjen, Scott Hanselman, Farhan Muhammad [同作者作品] [作译者介绍] 译者: 李敏波[同译者作品] ...
一个很不错的C#录音控件源码,如果觉得超值请好评。谢谢! 以下文字是帮您提供提示出错的解决方案 在用VS2005中编写一个非常简单的播放器程序时编译是OK的。当我Debug运行的时候遇到了问题,现象如下: LoaderLock ...
C# 2.0语言的改进1067 A.1 概述1067 A.2 泛型1068 A.2.1 泛型集合1068 A.2.2 泛型方法1072 A.3 迭代器1074 A.4 匿名方法1075 A.5 运算符重载1077 A.6 全局命名空间限定符1077 A.7 部分类1078 A.8 Visual Basic XML...
系统运行环境:Microsoft .NET Framework 2.0 + MS SQL 2000/2005 数据库Sql文件,执行即可。修改web.config数据库连接为您在使用的数据库名称。bin文件夹放置站点根目录! │ Ac.aspx 登陆 注册 退出 过程处理 ...
.NET Framework 4.5.2,.NET Framework 4.7.2和.NET Core 2.0中的OneDrive API 易于使用的轻量级框架,可通过Azure AD(api.onedrive.com&tenant-my.sharepoint.com/_api或Azure AD V2.0(graph.microsoft.com)与...
ITfoxtec.Identity.Saml2 ITfoxtec Identity Saml2软件包为身份提供程序(IdP)和依赖方(RP)添加了SAML-P支持。 支持.NET 5.0 支持.NET ... ITfoxtec Identity Saml2软件包已通过测试是否符合AD FS,Azure AD和Azu
本文主要介绍如何设置和配置Azure AD租户以及如何将Azure AD集成到ASP.NET Core 2.0 Web应用中以进行身份验证和基于角色的授权。
Azure AD B2C的身份示例 此存储库包含一个Visual Studio(代码)解决方案,为.NET开发人员演示现代的基于声明的身份方案,并特别关注使用进行身份验证和授权。 重要说明:此存储库中的代码尚未投入生产。 它仅用于...
活动目录dotnet-native-aspnetcore-v2 使用Microsoft身份平台保护ASP.NET Core Web API。 使用Microsoft身份平台保护ASP.NET Core Web API 关于这个样本 设想 在这种情况下,我们使用Microsoft身份平台保护Web API...
WPF Caliburn Micro + AvalonDock 2.0模板指南 这个项目是在我的之后诞生。 我花了很多时间来研究和收集问题和潜在的解决方案,以使WPF应用程序与Caliburn Micro和AvalonDock 2.0一起使用,即使这是最基本的情况。 ...
里面的程序包括USB驱动(可直接在电脑上安装的,32位和64位的),USB芯片CY7C68013A固件,FPGA程序(USB2.0速度测试和数字示波器的FPGA程序),上位机程序(使用C#,基于Visual Studio 2010)。硬件板子是在淘宝上买...
请在此演练帖子中找到更多信息: 为 Azure AD B2C 设置您的 ASP.NET Core 2.0 应用程序和服务 更多文档: 创建用户流 配置 网络应用 \\Authentication\\ { \\AzureAdB2C\\ { \\Instance\\ \\{your-tenant-name}.b2...
C#反编译的代码,可以深入研究以下。 功能概述 ADModify 使用 Microsoft Visual C#.NET 2003 编写。其 2.0 版本经过改进,执行相同修改时所花费的时间仅仅是先前版本所需时间的一半。其功能集允许管理员批量修改...
多租户和联合身份示例 存储库目标:补充有关多租户,微服务和联合身份的博客文章(请... 我将使用Google简化操作,您将需要设置Auth 2.0凭据,此Google页面介绍了如何执行此操作。 完成后,您应该会看到以下内容: 第6