Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 962
user3063548 Avatar asked Dec 03 '13 23:12

user3063548


2 Answers

I solved this problem by changing the IIS in Authentication

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

My IIS

like image 167
Vinícius Todesco Avatar answered Oct 16 '22 21:10

Vinícius Todesco


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

like image 20
Manuel Gs Avatar answered Oct 16 '22 22:10

Manuel Gs