visual-studio HttpContext.Current.User.Identity.Name 为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1571036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HttpContext.Current.User.Identity.Name is Empty
提问by Jordy
I have a Silverlight application (using MVC) and when i'm building in visual studio, using Visual Studio Development center, there's no problem, the HttpContext.Current.User.Identity.Namehas a Value
我有一个 Silverlight 应用程序(使用 MVC),当我在 Visual Studio 中构建时,使用 Visual Studio 开发中心,没有问题,HttpContext.Current.User.Identity.Name有一个值
But when i'm using the same project with IIS 7.5 (i'm using Windows 7), HttpContext.Current.User.Identity.Namestays empty
但是当我在 IIS 7.5 中使用同一个项目时(我使用的是 Windows 7),它HttpContext.Current.User.Identity.Name保持为空
Anyone who can help? Or knows where i can find the settings from the visual studio Development center, so i can check what's wrong in IIS?
任何人都可以提供帮助?或者知道在哪里可以找到 Visual Studio 开发中心的设置,以便我可以检查 IIS 中的问题?
回答by Bryan Bedard
I struggled with this problem the past few days.
过去几天我一直在努力解决这个问题。
I suggest reading Scott Guthrie's blog post Recipe: Enabling Windows Authentication within an Intranet ASP.NET Web application
我建议阅读 Scott Guthrie 的博客文章食谱:在 Intranet ASP.NET Web 应用程序中启用 Windows 身份验证
For me the problem was that although I had Windows Authentication enabled in IIS and I had <authentication mode="Windows" />in the <system.web>section of web.config, I was not preventing anonymous access. This last part was the key. You need to prevent anonymous access to ensure that the browser sends the credentials.
对我来说,问题是尽管我在 IIS 中启用了 Windows 身份验证并且我<authentication mode="Windows" />在<system.web>web.config 部分启用了,但我并没有阻止匿名访问。最后一部分是关键。您需要防止匿名访问以确保浏览器发送凭据。
You can either configure IIS in Control Panel so that your site (or machine) uses Windows authentication and denies anonymous access or you can add the following to your web.config in the system.web section:
您可以在控制面板中配置 IIS,以便您的站点(或机器)使用 Windows 身份验证并拒绝匿名访问,或者您可以将以下内容添加到 system.web 部分的 web.config 中:
<authentication mode="Windows" />
<authorization>
<deny users="?"/>
</authorization>
回答by digitally_inspired
These might resolve the issue(It did for me). In IIS Express change the project property values, "Anonymous Authentication" and "Windows Authentication". To do this, when project is selected, press F4 and then change these properties.
这些可能会解决问题(对我来说确实如此)。在 IIS Express 中更改项目属性值,“匿名身份验证”和“Windows 身份验证”。为此,在选择项目时,按 F4,然后更改这些属性。
In case you are deploying it on IIS locally, make sure local machines "Windows Authentication" feature is enabled and "Anonymous Authentication" is disabled.
如果您在本地 IIS 上部署它,请确保启用本地计算机的“Windows 身份验证”功能并禁用“匿名身份验证”。
Refer to
参考
https://grekai.wordpress.com/2011/03/31/httpcontext-current-user-identity-name-is-empty/
https://grekai.wordpress.com/2011/03/31/httpcontext-current-user-identity-name-is-empty/
回答by Dmitry Toder
In addition to "answered Mar 28 '11 at 12:27Bryan Bedard"
除了“回答 11 年 3 月 28 日 12:27 布赖恩·贝达德”
In case that the solution doesn't work, you have to enable Windows Authentication in iss manager. How to do that:
如果解决方案不起作用,您必须在 iss 管理器中启用 Windows 身份验证。怎么做:
1.To start IIS Manager from the Run dialog box: On the Start menu, click All Programs, click Accessories, and then click Run. In the Open box, type inetmgr and then click OK.
1.从“运行”对话框启动 IIS 管理器: 在“开始”菜单上,单击“所有程序”,单击“附件”,然后单击“运行”。在打开框中,键入 inetmgr,然后单击确定。
2.In the Connections pane, expand the server name, expand Sites, and go to the level in the hierarchy pane that you want to configure, and then click the Web site or Web application. 3. Scroll to the IIS section in the Home pane, and then double-click Authentication.
2.在连接窗格中,展开服务器名称,展开站点,然后转到层次结构窗格中要配置的级别,然后单击网站或Web 应用程序。3. 滚动到主页窗格中的 IIS 部分,然后双击身份验证。
4.In the Authentication pane, select Anonymous Authentication, and then click Disable.

4.在“身份验证”窗格中,选择“匿名身份验证”,然后单击“禁用”。

- In the Authentication pane, select Windows Authentication, and then click Enable.
- 在“身份验证”窗格中,选择“Windows 身份验证”,然后单击“启用”。
回答by PaulTheCyclist
I also had this problem recently. Working with a new client, trying to get a an old web forms app running from Visual Studio, with IISExpress using Windows Authentication. For me, the web.config was correctly configured
我最近也有这个问题。使用新客户端,尝试使用使用 Windows 身份验证的 IISExpress 从 Visual Studio 运行旧的 Web 表单应用程序。对我来说,web.config 配置正确
However, the IISExpress.config settings file had:
但是,IISExpress.config 设置文件具有:
<windowsAuthentication enabled="false">
The user account the developer was logged in was very new, so unlikley it had been edited. Simple fix it turned out, change this to enabled=true and it all ran as it should then.
开发人员登录的用户帐户很新,因此不太可能对其进行编辑。原来是简单的修复,将其更改为 enabled=true ,然后一切都按原样运行。
回答by Adarsh
Disabling all other options in authentication tab of iis except windows authentication resolved my issue. Please check..
在 iis 的身份验证选项卡中禁用除 Windows 身份验证之外的所有其他选项解决了我的问题。请检查..
Steps:
脚步:
- Open iis in your machine
- Select your application from the application pool
- Click on authentication option
- Disable all other option except windows authentication (Anonimous authentication should be disabled)
- 在你的机器上打开 iis
- 从应用程序池中选择您的应用程序
- 单击身份验证选项
- 禁用除 Windows 身份验证之外的所有其他选项(应禁用匿名身份验证)
Please check this and let me know the feedback. It worked for me. hope it will work for you also..

回答by Mishax
Also, especially if you are a developer, make sure that you are in fact connecting to the IIS server and not to the IIS Express that comes with Visual Studio. If you are debugging a project, it's just as easy if not easier sometimes to think you are connected to IIS when you in fact aren't.
此外,特别是如果您是开发人员,请确保您实际上是连接到 IIS 服务器,而不是连接到 Visual Studio 附带的 IIS Express。如果您正在调试一个项目,那么有时会认为您已连接到 IIS,而实际上实际上并没有连接到 IIS,即使不是更容易,也同样容易。
Even if you've enabled Windows Authentication and disabled Anonymous Authentication on IIS, this won't make any difference to your Visual Studio simulation.
即使您在 IIS 上启用了 Windows 身份验证并禁用了匿名身份验证,这也不会对您的 Visual Studio 模拟产生任何影响。
回答by cjk
The browser will only detect your username if the IIS server is on the same domain and the security settings within your group policy allow it.
只有当 IIS 服务器位于同一域中并且组策略中的安全设置允许时,浏览器才会检测到您的用户名。
Otherwise you will have to provide it with credentials, but if it is not on the same domain, it will not be able to authenticate you.
否则,您必须向它提供凭据,但如果它不在同一个域中,它将无法对您进行身份验证。
回答by cornhedgehog
Apart from all obvious reasons mentioned earlier, there might be another one: you didn't put an Authorize attribute on top of your controller, like that:
除了前面提到的所有明显原因之外,可能还有另一个原因:您没有在控制器顶部放置 Authorize 属性,如下所示:
[Authorize(Roles = "myRole")]
[EnableCors(origins: "http://localhost:8080", headers: "*", methods: "*", SupportsCredentials = true)]
public class MyController : ApiController
At least that's what worked for me.
至少这对我有用。
回答by waxingsatirical
As @PaulTheCyclist says, If using IISExpress anonymous authentication is enabled by default, windows authentication is disabled.
正如@PaulTheCyclist 所说,如果默认启用 IISExpress 匿名身份验证,则禁用 Windows 身份验证。
This can be changed in what I'm sure used to be called PropertyPages (NOT right-click -> properties). Select the web project
这可以在我确信过去称为 PropertyPages 的地方进行更改(不是右键单击 - > 属性)。选择网络项目

