HttpContext.Current.User.Identity.Name 当Visual Studio设置为"使用本地IIS Web服务器"时为空/空白,但在Visual Studio设置为"使用Visual Studio开发服务器"时正常工作.
希望您可以通过执行以下操作在Visual Studio 2010或2012中复制此问题(我已尝试过两者):
创建一个选中".NET Framework 4"的新"ASP.NET空Web应用程序",并将其命名为"WindowsAuthTest2".用.替换Web.config的内容
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="Windows">
</authentication>
</system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)
接下来,在名为"Default.aspx"的项目中添加一个新的"Web窗体".用Default替换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>
Run Code Online (Sandbox Code Playgroud)
并用Default替换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 + "]"; …Run Code Online (Sandbox Code Playgroud)