首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用IIS Express但不使用Visual Studio Development Server时,HttpContext.Current.User.Identity.Name为空

使用IIS Express但不使用Visual Studio Development Server时,HttpContext.Current.User.Identity.Name为空
EN

Stack Overflow用户
提问于 2013-12-04 07:21:42
回答 2查看 69.4K关注 0票数 22

当Visual Studio设置为“使用本地IIS Web服务器”时,HttpContext.Current.User.Identity.Name为空/空白,但当Visual Studio设置为“使用Visual Studio开发服务器”时,它可以正常工作。

希望您可以通过执行以下操作,在Visual Studio 2010或2012 (我已经尝试过这两个版本)中复制此问题:

创建一个新的"ASP.NET Empty Web Application“,并选择".NET Framework4”并将其命名为"WindowsAuthTest2“。将Web.config的内容替换为

代码语言:javascript
复制
<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <authentication mode="Windows">
    </authentication>
  </system.web>
</configuration>

接下来,向项目中添加一个名为"Default.aspx“的新"Web Form”。将Default.aspx中的代码替换为

代码语言:javascript
复制
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WindowsAuthTest2.Default" %>

<html>
    <head>
        <title></title>
    </head>
    <body>
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </body>
</html>

并将Default.aspx.cs中的代码替换为

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WindowsAuthTest2
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = "[" + HttpContext.Current.User.Identity.Name + "]";
        }
    }
}

我没有ReSharper来告诉我哪些使用语句是不必要的。

然后,您可以通过右键单击项目,选择"Properties“(或双击项目下的"Properties”),选择" web ",并导航到"Servers“部分,来选择Visual Studio将使用的Web服务器。

尝试使用“使用Visual Studio开发服务器”和“使用本地IIS Web服务器”。

希望您能帮助我弄清楚为什么在选择"Use Local IIS Web server“时HttpContext.Current.User.Identity.Name是空的。

EN

回答 2

Stack Overflow用户

发布于 2015-11-11 20:59:23

我通过更改身份验证中的IIS解决了这个问题

代码语言:javascript
复制
Windows Authenticaon = Enabled
Anonymous Authentication = DISABLED <=== Important
ASP.NET Impersonation Enabled

My IIS

票数 10
EN

Stack Overflow用户

发布于 2014-12-19 08:52:12

我已经解决了!您需要对文件夹中的NETWORK、NETWORK SERVICE和IIUSRS赋予权限。并禁止匿名访问。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20363766

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档