如何使用 .NET Framework 获取 DomainName\AccountName?
我怎样才能得到
域名\帐户名
作为 .NET Framework 的字符串?
How can i get the
DomainName\AccountName
as string with the .NET Framework?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
Environment.UserDomainName
< /a> 属性来检索域和Environment.UserName
检索用户名:You can use the
Environment.UserDomainName
property to retrieve the domain andEnvironment.UserName
to retrieve the user name: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, useHttpContext.Current.User.Identity.Name
instead.如果您使用 ASP.NET,您可以使用
if you are using ASP.NET you can use
WindowsIdentity.GetCurrent().Name
WindowsIdentity.GetCurrent().Name