如何使用 .NET Framework 获取 DomainName\AccountName?

发布于 2024-08-30 13:40:40 字数 90 浏览 2 评论 0原文

我怎样才能得到

域名\帐户名

作为 .NET Framework 的字符串?

How can i get the

DomainName\AccountName

as string with the .NET Framework?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

迷爱 2024-09-06 13:40:40
System.Security.Principal.WindowsIdentity.GetCurrent().Name;
System.Security.Principal.WindowsIdentity.GetCurrent().Name;
Bonjour°[大白 2024-09-06 13:40:40

您可以使用 Environment.UserDomainName< /a> 属性来检索域和 Environment.UserName 检索用户名:

Dim domainAndUserName As String _
    = Environment.UserDomainName & "\\" & Environment.UserName

You can use the Environment.UserDomainName property to retrieve the domain and Environment.UserName to retrieve the user name:

Dim domainAndUserName As String _
    = Environment.UserDomainName & "\\" & Environment.UserName
oО清风挽发oО 2024-09-06 13:40:40

Environment.UserDomainName 包含您的帐户加入的域/计算机名称。 Environment.UserName 仅包含用户名。要获得您想要的结果,您需要连接变量(Environment.UserDomainName & "\\" & Environment.UserName)。不过,这只适用于本地上下文,如果您在网站中使用此代码,您将获得运行应用程序池的帐户名称。在 asp.net 中,请使用 HttpContext.Current.User.Identity.Name 代替。

Environment.UserDomainName contains the domain/computer name that your account is joined to. Environment.UserName contains 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.Name instead.

江南月 2024-09-06 13:40:40

如果您使用 ASP.NET,您可以使用

HttpContext.Current.User.Identity.Name;

if you are using ASP.NET you can use

HttpContext.Current.User.Identity.Name;
旧故 2024-09-06 13:40:40

WindowsIdentity.GetCurrent().Name

WindowsIdentity.GetCurrent().Name

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文