2016-06-13 111 views
0

这可能是重复问题,但我没有从其他帖子中找到任何解决方案。HttpContext.Current.User.Identity.Name每次都是空的

我正在使用Windows身份验证,当我在本地系统上构建应用程序时,它的工作正常,但是当我在IIS上部署应用程序时,HttpContext.Current.User.Identity.Name返回null。我已禁用身份验证设置以外的Windows身份验证。

var strUserName = HttpContext.Current.User.Identity.Name == "" ? System.Security.Principal.WindowsIdentity.GetCurrent().Name : HttpContext.Current.User.Identity.Name; 

我得到strUserName为空。

<authentication mode="Windows"> 
</authentication> 

<identity impersonate="true"/> 

我试图划分线和编写代码来检查究竟是什么returing ...

HttpContext.Current.User.Identity.Name is returning null 
System.Security.Principal.WindowsIdentity.GetCurrent().Name is returning NT AUTHORITY\IUSR when impersonate is true 
System.Security.Principal.WindowsIdentity.GetCurrent().Name is returning IIS APPPOOL\ApplicationPoolName when impersonate is false 

以下几个环节我已经检查

HttpContext.Current.User.Identity.Name is empty using IIS Express but not Visual Studio Development Server

User.Identity.Name with windows authentication

+1

我想向您推荐[编辑]您的问题,并添加您发现的其他类似问题,并尽量避免将您的问题标记为重复。 – kayess

+0

我相信第二个''标签会覆盖上面例子中的第一个,所以'mode = Windows'不会被拾取。你可以尝试合并两个配置项目吗? –

+0

您是指服务/ WCF? –

回答

0

在Visual Studio Solution Explorer中 - Cli ck在web应用程序项目上 - >在底部选择属性选项卡。请确保您有以下设置:

  1. 匿名验证| 已禁用
  2. Windows身份验证| 启用

改变你的web.config这样的:

<system.web> 
    <authentication mode="Windows"/> 
    <compilation debug="true" targetFramework="4.5"/> 
    <httpRuntime/> 
    <pages controlRenderingCompatibilityVersion="4.0"/> 
    </system.web> 
    <system.webServer> 
    <modules> 
     <remove name="FormsAuthentication"/> 
    </modules> 
    </system.webServer> 

现在尝试访问当前的Windows用户是这样的:

string user = System.Web.HttpContext.Current.User.Identity.Name; 
+0

设置与您的建议相同,当我清理浏览器历史记录并将匿名身份验证重新设为禁用时,它将开始显示用户名密码窗口,但它不接受凭据,我可以使用相同的凭据获取远程。它不应该直接显示任何弹出窗口,它必须检查用户并重定向到默认屏幕。 – Rocky

+0

你使用MVC吗? –

+0

没有它简单的asp.net应用程序 – Rocky