Environment.UserName返回应用程序池名称而不是用户名 [英] Environment.UserName returning application pool name instead of username

查看:144
本文介绍了Environment.UserName返回应用程序池名称而不是用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下一行

Environment.UserName

在Visual Studio的调试模式下,像我需要的那样返回用户的身份.

In debug mode in visual studio returns the identity of the user like I need.

然后,当我在IIS中设置站点并运行代码时,同一行将返回该站点使用的应用程序池的名称.

Then when I set up my site in IIS and run the code the same line returns the name of the application pool which the site uses.

如何获取仍返回用户名的信息?

How can I get it to still return the user name ?

推荐答案

尝试如下操作:

if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
   string username = System.Web.HttpContext.Current.User.Identity.Name;
}

重要说明:您需要配置IIS以启用集成安全性并禁用匿名登录.

Important note: You need to configure IIS to enable integrated security and disable anonymous logon.

请注意,Environment.Username返回当前线程上的用户名.

Note that Environment.Username returns the Username on the current Thread.

这篇关于Environment.UserName返回应用程序池名称而不是用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆