网站导航免费论文 原创论文 论文搜索 原创论文 网学软件 学术大家 资料中心 会员中心 问题解答 原创论文 大学论文导航 设计下载 最新论文 下载排行 原创论文 论文源代码
返回网学首页
网学联系
最新论文 推荐专题 热门论文 素材专题
当前位置: 网学 > 编程文档 > ASP.net > 正文

细说ASP.NET Windows身份认证

来源:http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 13/01/05
;这个域中搜索某个用户信息,我们可以使用下面的语句构造一个DirectoryEntry对象:

  1. DirectoryEntry entry = new DirectoryEntry("LDAP://test.corp"); 

在这段代码中,我采用硬编码的方式把域名写进了代码。

我们如何知道当前电脑所使用的是哪个域名呢?

答案是:查看“我的电脑”的属性对话框:

498)this.width=498;'' onmousewheel = ''javascript:return big(this)'' alt="" src="/uploadfile/201301/5/BA151816106.png" />

注意:这个域名不一定与System.Environment.UserDomainName相同。

除了可以查看“我的电脑”的属性对话框外,我们还可以使用代码的方式获取当前电脑所使用的域名:

  1. private static string GetDomainName()  
  2. {  
  3.     // 注意:这段代码需要在Windows XP及较新版本的操作系统中才能正常运行。  
  4.     SelectQuery query = new SelectQuery("Win32_ComputerSystem");  
  5.     using( ManagementObjectSearcher searcher = new ManagementObjectSearcher(query) ) {  
  6.         foreach( ManagementObject mo in searcher.Get() ) {  
  7.             if( (bool)mo["partofdomain"] )  
  8.                 return mo["domain"].ToString();  
  9.         }  
  10.     }  
  11.     return null;  

当构造了DirectorySearcher对象后,我们便可以使用DirectorySearcher来执行对Active Directory的搜索。

我们可以使用下面的步骤来执行搜索:

1. 设置 DirectorySearcher.Filter 指示LDAP格式筛选器,这是一个字符串。

2. 多次调用PropertiesToLoad.Add() 设置搜索过程中要检索的属性列表。

3. 调用FindOne() 方法获取搜索结果。

下面的代码演示了如何从Active Directory中搜索登录名为“fl45”的用户信息:

  1. static void Main(string[] args)  
  2. {  
  3.     Console.WriteLine(Environment.UserDomainName);  
  4.     Console.WriteLine(Environment.UserName);  
  5.     Console.WriteLine("------------------------------------------------");  
  6.  
  7.     ShowUserInfo("fl45", GetDomainName());  
  8. }  
  9.  
  10. private static string AllProperties = "name,givenName,samaccountname,mail";  
  11.  
  12. public static void ShowUserInfo(string loginName, string domainName)  
  13. {  
  14.     ifstring.IsNullOrEmpty(loginName) || string.IsNullOrEmpty(domainName) )  
  15.         return;  
  16.  
  17.     string[] properties = AllProperties.Split(new char[] { ''\r''''\n'''','' },   
  18.                         StringSplitOptions.RemoveEmptyEntries);  
  19.  
  20.     try {  
  21.         DirectoryEntry entry = new DirectoryEntry("LDAP://" + domainName);  
  22.         DirectorySearcher search = new DirectorySearcher(entry);  
  23.         search.Filter = "(samaccountname=" + loginName + ")";  
  24.  
  25.         foreachstring p in properties )  
  26.             search.PropertiesToLoad.Add(p);  
  27.  
  28.         SearchResult result = search.FindOne();  
  29.  
  30.         if( result != null ) {  
  31.             foreachstring p in properties ) {  
  32.                 ResultPropertyValueCollection collection = result.Properties[p];  
  33.                 forint i = 0; i < collection.Count; i++ )  
  34.                     Console.WriteLine(p + ": " + collection[i]);  
  35.             }  
  36.         }  
  37.     }  
  38.     catch( Exception ex ) {  
  39.         Console.WriteLine(ex.ToString());  
  40.     }  

网学推荐

免费论文

原创论文

设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号