C#中的登录名 [英] login name in c#
本文介绍了C#中的登录名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨团队
有两件事的用户名和该用户的名称.我们可以通过
获得用户名
Hi Team
There are 2 thing username and name of that user. we can get username by
Environment.GetEnvironmentVariable("USERNAME");
但是,我们应该如何获取该用户的名称(考虑到两者是不同的)
在此先感谢
问候
Arihant
But how should we get the name of that user(considering both are different)
Thanks in advance
Regards
Arihant
推荐答案
看看System.Security.Principal;
例如
Have a look at System.Security.Principal;
e.g.
WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
Console.WriteLine(principal.Identity.Name);
Console.WriteLine(string.Format("User is administrator = {0}", principal.IsInRole("Administrators")));
然后,您可以查询Active Directory以检索有关该用户的其他信息
You could then query Active Directory to retrieve additional information about the user
using(DirectoryEntry de = new DirectoryEntry("LDAP://MyDomainController"))
{
using(DirectorySearcher adSearch = new DirectorySearcher(de))
{
adSearch.PropertiesToLoad.Add("sn"); // surname = last name
adSearch.PropertiesToLoad.Add("givenName"); // given (or first) name
adSearch.PropertiesToLoad.Add("mail"); // e-mail addresse
adSearch.PropertiesToLoad.Add("telephoneNumber"); // phone number
adSearch.Filter = "(sAMAccountName=someuser)";
SearchResult adSearchResult = adSearch.FindOne();
}
}
这篇关于C#中的登录名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文