当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的内容替换为
<?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中的代码替换为
<%@ 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来告诉我哪些使用语句是不必要的。
然后,您可以通过右键单击项目,选择"Properties“(或双击项目下的"Properties”),选择" web ",并导航到"Servers“部分,来选择Visual Studio将使用的Web服务器。
尝试使用“使用Visual Studio开发服务器”和“使用本地IIS Web服务器”。
希望您能帮助我弄清楚为什么在选择"Use Local IIS Web server“时HttpContext.Current.User.Identity.Name
是空的。
发布于 2015-11-11 20:59:23
我通过更改身份验证中的IIS解决了这个问题
Windows Authenticaon = Enabled
Anonymous Authentication = DISABLED <=== Important
ASP.NET Impersonation Enabled
发布于 2014-12-19 08:52:12
我已经解决了!您需要对文件夹中的NETWORK、NETWORK SERVICE和IIUSRS赋予权限。并禁止匿名访问。
https://stackoverflow.com/questions/20363766
复制相似问题