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

半世苍凉 提交于 2019-12-19 05:04:28

问题


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.


回答1:


I solved this problem by changing the IIS in Authentication

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

My IIS




回答2:


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



来源:https://stackoverflow.com/questions/20363766/httpcontext-current-user-identity-name-is-empty-using-iis-express-but-not-visual

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!