HttpContext.Current.User.Identity.Name is empty using IIS Express but not Visual Studio Development Server

user3063548 picture user3063548 · Dec 4, 2013 · Viewed 68.6k times · Source

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.

Answer

Vin&#237;cius Todesco picture Vinícius Todesco · Nov 11, 2015

I solved this problem by changing the IIS in Authentication

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

My IIS