嗯,需要写一个需求管理工具。
Winform,免登陆
头疼,需要抓到User Name 公司电脑都是加域,
所以登录的时候会显示中文名
于是乎,我试着抓取本机的用户名,然而出来的是账户名,也就是登录的英文名
百思不得其解,然后看到一段获取域用户资料的代码,似乎有了点头绪
//添加程序集引用
System.DirectoryServices.AccountManagement
IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
string hostName = ipGlobalProperties.HostName;
string domainName = ipGlobalProperties.DomainName;
//获取指定域中的用户的查找对象
PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, domainName);
UserPrincipal userPrincipal = new UserPrincipal(principalContext);
PrincipalSearcher principalSearcher = new PrincipalSearcher(userPrincipal);
//查找域中用户及其信息。
StringBuilder sb = new StringBuilder();
foreach (UserPrincipal userPrincipalSearchResult in principalSearcher.FindAll())
{
sb.AppendLine(string.Format("UPN:{0}", userPrincipalSearchResult.UserPrincipalName));
sb.AppendLine(string.Format("姓氏Last Name:{0}", userPrincipalSearchResult.Surname));
sb.AppendLine(string.Format("中间名:{0}", userPrincipalSearchResult.MiddleName));
sb.AppendLine(string.Format("Given Name/First Name名:{0}", userPrincipalSearchResult.GivenName));
sb.AppendLine(string.Format("名称:{0}", userPrincipalSearchResult.Name));
sb.AppendLine(string.Format("上次登录时间:{0}", userPrincipalSearchResult.LastLogon));
}
userPrincipal.Dispose();
Console.WriteLine(sb.ToString());
查找域中计算机及其信息,类似于查找用户。首先使用域上下文对象实例化一个计算机对象,然后使用该对象实例化一个查找对象。
查找的结果,即为计算机对象,循环获取信息即可。
ComputerPrincipal computerPrincipal = new ComputerPrincipal(principalContext);
principalSearcher = new PrincipalSearcher(computerPrincipal);
//
foreach (ComputerPrincipal computerPrincipalSearchResult in principalSearcher.FindAll())
{
sb.AppendLine(string.Format("UPN:{0}", computerPrincipalSearchResult.UserPrincipalName));
sb.AppendLine(string.Format("描述:{0}", computerPrincipalSearchResult.Description));
sb.AppendLine(string.Format("是否启用:{0}", computerPrincipalSearchResult.Enabled));
sb.AppendLine(string.Format("SAM账号名:{0}", computerPrincipalSearchResult.SamAccountName));
sb.AppendLine(string.Format("名称:{0}", computerPrincipalSearchResult.Name));
sb.AppendLine(string.Format("上次登录时间:{0}", computerPrincipalSearchResult.LastLogon));
}
computerPrincipal.Dispose();
Console.WriteLine(sb.ToString());
嗯哼,稍微改了下
IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties(); //初始化IPGlobalProperties
//string hostName = ipGlobalProperties.HostName; //獲取電腦名稱,EX: UK11_B3PC56
string domainName = ipGlobalProperties.DomainName; //獲取網域名,EX: flexium.com.cn or flexium.com.tw
PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, domainName); //使用指定的內容型別、名稱和容器,初始化 PrincipalContext 類別的新執行個體
UserPrincipal userPrincipal = new UserPrincipal(principalContext); //初始化 UserPrincipal
//將本機用戶名和網域合併為郵箱地址, ex : maosheng_zhou@flexium.com.cn 賦值給UserPrincipalName
userPrincipal.UserPrincipalName = System.Environment.UserName+"@" + domainName;
PrincipalSearcher principalSearcher = new PrincipalSearcher(userPrincipal); //初始化PrincipalSearcher搜尋模式
StringBuilder sb = new StringBuilder();
foreach (UserPrincipal userPrincipalSearchResult in principalSearcher.FindAll()) //在principalSearcher的結果里搜索 UserPrincipal值等於UserPrincipalName的條目
{
sb.AppendLine(string.Format("{0}", userPrincipalSearchResult.Name)); //只顯示Name值
}
userPrincipal.Dispose(); //處置 userPrincipal 物件目前的執行個體
label4.Text = sb.ToString().Trim(); //將Name值在label中顯示
然后就完美的出来了~
此文仅记录,供后来者参考。
评论区