我试图找出如何在webapi中找到已登录用户的身份。在开发服务器和旧版IIS7上,身份在多个位置可用,但是当我尝试在IIS8上设置我的应用程序时,所有已知来源都是空白的。
使用<authentication mode="Windows" />
我通常可以在以下位置看到用户身份:
System.Web.HttpContext.Current.User.Identity.Name
User.Identity.Name
Thread.CurrentPrincipal.Identity.Name
但是在IIS8上,它们都是空的。
有什么想法,或者如何配置IIS8或我的应用程序来查找用户身份?
答案 0 :(得分:1)
ApiController.User
是你需要的。 Web API没有System.Web,因此它的行为方式不同。
答案 1 :(得分:0)
您正在与匿名用户进行身份验证
您可以通过修改web.config
:
<system.web>
<authorization>
<deny users="?" /> <!-- The question-mark represents the `anonymous`-user) -->
</authorization>
</system.web>
通常当System.Web.HttpContext.Current.User.Identity.Name
为空时,Windows身份验证失败。看看HttpContext.Current.Request.IsAuthenticated
。
比较IIS7和IIS8之间的设置 (app-pool,web.config以及 IIS-Authentication设置)
另一个问题可能是您正在使用的客户端(浏览器及其设置)。 也许浏览器没有提供Windows身份。
对于 Internet Explorer ,请验证:
Settings -> Advanced -> "Enable Integrated Windows Authentication*"