我如何获得当前用户名在。net使用c# ?
当前回答
你可能还想尝试使用:
Environment.UserName;
像这样…:
string j = "Your WindowsXP Account Name is: " + Environment.UserName;
希望这对你有所帮助。
其他回答
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
使用实际登录用户的System.Windows.Forms.SystemInformation.UserName作为环境。UserName仍然返回当前进程正在使用的帐户。
我从已有的答案中尝试了几种组合,但它们给了我
DefaultAppPool
IIS APPPOOL
IIS APPPOOL\DefaultAppPool
最后我用了
string vUserName = User.Identity.Name;
这给了我实际的用户域用户名。
Use:
System.Security.Principal.WindowsIdentity.GetCurrent().Name
这将是登录名。
我完全赞同其他的答案,但我想强调另一个方法,它说
String UserName = Request.LogonUserIdentity.Name;
上述方法返回的用户名格式为:DomainName\ username。例如,EUROPE\UserName
这不同于:
String UserName = Environment.UserName;
显示格式为:UserName
最后:
String UserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
它给出:NT AUTHORITY\IUSR(在IIS服务器上运行应用程序时)和DomainName\UserName(在本地服务器上运行应用程序时)。
推荐文章
- 实体框架核心:在上一个操作完成之前,在此上下文中开始的第二个操作
- 如何为构造函数定制Visual Studio的私有字段生成快捷方式?
- 为什么Visual Studio 2015/2017/2019测试运行器没有发现我的xUnit v2测试
- 如何使用JSON确保字符串是有效的JSON。网
- AppSettings从.config文件中获取值
- 通过HttpClient向REST API发布一个空体
- 如何检查IEnumerable是否为空或空?
- 自动化invokerrequired代码模式
- 没有ListBox。SelectionMode="None",是否有其他方法禁用列表框中的选择?
- 在c#代码中设置WPF文本框的背景颜色
- 在c#中,什么是单子?
- c#和Java中的泛型有什么不同?和模板在c++ ?
- c#线程安全快速(est)计数器
- 如何将此foreach代码转换为Parallel.ForEach?
- 如何在iis7应用程序池中设置。net Framework 4.5版本