利用DirectoryEntry组件来查看网络,显示域用户的列表, 查询客户端域账户

来源:互联网 发布:网络广告设计教程 编辑:程序博客网 时间:2024/05/01 16:22

摘要
System.DirectoryServices.DirectoryEntry组件提供了对Active Directory的访问。本文以两个简单的小程序为例,阐述了如何利用此组件查看网络的各节点的信息。


目录
问题的提出
问题的初步解决
改进后的Windows Forms方案
总结
参考文献
附注

作者


问题的提出
刚接触.Net网络编程的时候,我常想,有没有办法列出局域网中的所有计算机呢?直到最近看了MSDN中关于DirectoryEntry 类的介绍,这才找到了答案。
问题的初步解决
DirectoryEntry组件提供了Path属性,根据文档,此属性指定了目录服务中用来访问对象的对象名,其格式如下:
protocol://servername:port number/distinguished name
此语句的第一部分定义了访问将使用的协议,如
LDAP: (Lightweight Directory Access Protocol)
IIS: (提供IIS元数据来读及配置Internet Infomation Server)
WinNT: (提供在非常有限的性能下对Windows NT域的访问)
NDS: (提供对Novell Directory Service的访问)
等等(详细信息清参考MSDN)。
据此,我们构造了一个DirectoryEntry实例,将它的Path设为"WinNT:",以通过对它的所有子项的枚举来发现网络上的所有域(以及工作组)。这样,再对所发现的域(以及工作组)的子项进行枚举,就可以发现网络上的所有计算机。下面的一个控制台小程序演示了这一点。


using System;
using System.DirectoryServices;

 

class TempClass
{
static void Main()
{
EnumComputers();
}

static void EnumComputers()
{
using(DirectoryEntry root = new DirectoryEntry("WinNT:"))
{
foreach(DirectoryEntry domain in root.Children)
{
Console.WriteLine("Domain | WorkGroup:/t"+domain.Name);
foreach(DirectoryEntry computer in domain.Children)
{
Console.WriteLine("Computer:/t"+computer.Name);
}
}
}
}
}


改进后的Windows Forms方案
上面代码中两个嵌套的foreach循环看起来并不是太好,并且控制台的显示效果也并不那么美观。下面,我将对代码进行一些改动,并将它移植到WinForm上。
新建一个Windows Application[C#],在Form上添加一个TreeView,命名为treeView1。
添加以下几个函数:


//用指定的文本构造一个节点,将其添加为参数parant的子节点,并返回刚构造的节点
private TreeNode AddNode(TreeNode parant,string text)
{
TreeNode node = new TreeNode(text);
parant.Nodes.Add(node);
return node;
}

 

//递归地找到参数entry的所有子节点,并在treeView1中显示;这里的entry与entryNode需相对应
private void EnumChildren(DirectoryEntry entry,TreeNode entryNode)
{
if(entry.Children!=null)//如果无子节点则结束
{
foreach(DirectoryEntry i in entry.Children)
{
//将各子节点加入TreeView,并进行递归
EnumChildren(i,AddNode(entryNode,i.Name));
}
}
}

//用给定的字符串构造根节点,并列出其所有子节点
private void Enumerate(string path)
{
try
{
using(DirectoryEntry root = new DirectoryEntry(path))
{
TreeNode node = new TreeNode(root.Name);
treeView1.Nodes.Add(node);
EnumChildren(root,node);
}
}
catch {}
}


 

这样,通过传递 "WinNT:" 给函数Enumerate(string),就可以在TreeView中看到网络上的所有计算机,以及每台计算机上的用户、组、服务等资源

 


 

总结
本文主要介绍了用DirectoryEntry组件来浏览网络中的各节点计算机的信息,实际上,DirectoryEntry组件功能强大,例如将"IIS:"作为DirectoryEntry的Path属性,就可以列出域中运行着IIS(Internet Infomation Server)的服务器,并可获得IIS元数据等属性;此外,还可以用它来对网络进行远程管理与配置,有兴趣者不妨一试。

 

 

 

asp.net获取客户端域账户



 

step 1
<script language=javascript>
var wshNetwork = new ActiveXObject("WScript.Network");
alert("域名 = "+ wshNetwork.UserDomain);
alert("计算机名 = "+ wshNetwork.ComputerName);
alert("登录用户名 = "+ wshNetwork.UserName);
</script>


step2
1、打开IE浏览器,找到工具菜单
open the IE and click the "tool" on the menu bar



2、点击IE属性
Click the "Internet Option "



3、切换到安全页,选择Internet,点击自定义级别按钮
select the tag of "security" -> "Internet" and then click the button" Custom Level"



4、找到用户认证,选择自动使用当前用户名密码登录,点击确定按钮,再次点击确定关闭窗口Browse and find the "User Authentication ", select the "Automatic logon with current username and password". And Click "OK"

 

原创粉丝点击