【问题标题】:HttpContext.Current.User.Identity.Name is empty using IIS Express but not Visual Studio Development Server [duplicate]HttpContext.Current.User.Identity.Name 使用 IIS Express 为空,但不是 Visual Studio Development Server [重复]
【发布时间】:2013-12-03 23:21:41
【问题描述】:

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

希望您可以通过执行以下操作在 Visual Studio 2010 或 2012(我都尝试过)中复制此问题:

创建一个新的“ASP.NET 空 Web 应用程序”并选择“.NET Framework 4”并将其命名为“WindowsAuthTest2”。将 Web.config 的内容替换为

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <authentication mode="Windows">
    </authentication>
  </system.web>
</configuration>

接下来将一个新的“Web 表单”添加到名为“Default.aspx”的项目中。将 Default.aspx 中的代码替换为

<%@ 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 中的代码替换为

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 告诉我哪些 using 语句是不必要的。

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

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

希望您能帮我弄清楚为什么在选择“使用本地 IIS Web 服务器”时HttpContext.Current.User.Identity.Name 为空。

【问题讨论】:

  • 要上传图片,请尝试上传到 imgur.com,然后在此处发布图片链接。
  • 仅供参考:您也不需要 ReSharper 来清理您的 using 语句。见SCREENSHOT

标签: c# asp.net visual-studio identity


【解决方案1】:

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

Windows Authenticaon = Enabled
Anonymous Authentication = DISABLED <=== Important
ASP.NET Impersonation Enabled

My IIS

【讨论】:

  • 它解决了我的问题..谢谢:)
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-13
    • 2011-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多