HttpContext.Current.User.Identity.Name is empty us

2020-03-08 07:56发布

HttpContext.Current.User.Identity.Name is empty/blank when Visual Studio is set to "Use Local IIS Web server" but works correctly when Visual Studio is set to "Use Visual Studio Development Server".

Hopefully you can duplicate this problem in Visual Studio 2010 or 2012 (I have tried both) by doing the following:

Create a new "ASP.NET Empty Web Application" with ".NET Framework 4" selected and name it "WindowsAuthTest2". Replace the contents of Web.config with

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

Next add a new "Web Form" to the project called "Default.aspx". Replace the code in Default.aspx with

<%@ 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>

And replace the code in Default.aspx.cs with

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 + "]";
        }
    }
}

I don't have ReSharper to tell me which of those using statements are unnecessary.

Then you can select which web server Visual Studio will use by right-clicking the project, selecting "Properties" (or by double clicking "Properties" under the project), selecting "Web", and navigating to the "Servers" section.

Try it with "Use Visual Studio Development Server" and "Use Local IIS Web server".

Hopefully you can help me figure out why HttpContext.Current.User.Identity.Name is empty when "Use Local IIS Web server" is selected.

2条回答
啃猪蹄的小仙女
2楼-- · 2020-03-08 08:02

I have solved it! you need to give permission to "NETWORK", "NETWORK SERVICE" and IIUSRS in the folder. And disable the anonymous access.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2020-03-08 08:12

I solved this problem by changing the IIS in Authentication

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

My IIS

查看更多
登录 后发表回答