如何使用 .NET Framework 获取 DomainName\AccountName?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2719362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i get the DomainName\AccountName with the .NET Framework?
提问by OrElse
How can i get the
我怎样才能得到
DomainName\AccountName
域名\账户名
as string with the .NET Framework?
作为 .NET Framework 的字符串?
回答by codingbadger
System.Security.Principal.WindowsIdentity.GetCurrent().Name;
回答by Dirk Vollmar
You can use the Environment.UserDomainNameproperty to retrieve the domain and Environment.UserNameto retrieve the user name:
您可以使用该Environment.UserDomainName属性来检索域并Environment.UserName检索用户名:
Dim domainAndUserName As String _
= Environment.UserDomainName & "\" & Environment.UserName
回答by Eric Johansson
Environment.UserDomainNamecontains the domain/computer name that your account is joined to. Environment.UserNamecontains only the username. To get the result you're after, you need to concaternate the variables(Environment.UserDomainName & "\\" & Environment.UserName). This only works well in a local context though, if you use this code in a website, you'll get the account name that your application pool is running under. In asp.net, use HttpContext.Current.User.Identity.Nameinstead.
Environment.UserDomainName包含您的帐户加入的域/计算机名称。Environment.UserName只包含用户名。为了得到你想要的结果,你需要连接变量( Environment.UserDomainName & "\\" & Environment.UserName)。不过,这只适用于本地上下文,如果您在网站中使用此代码,您将获得运行应用程序池的帐户名称。在 asp.net 中,HttpContext.Current.User.Identity.Name改为使用。
回答by Fahad
if you are using ASP.NET you can use
如果您使用的是 ASP.NET,则可以使用
HttpContext.Current.User.Identity.Name;
回答by Jens Granlund
WindowsIdentity.GetCurrent().Name
WindowsIdentity.GetCurrent().Name

