1

It may be duplication question but I did not found any solution from other post.

I am using Windows authentication, when I am building application on local system its working fine but when I am deploying the application on IIS HttpContext.Current.User.Identity.Name is returning null. I have disable eveything except Windows authentication on authentication setting.

var strUserName = HttpContext.Current.User.Identity.Name == "" ? System.Security.Principal.WindowsIdentity.GetCurrent().Name : HttpContext.Current.User.Identity.Name;

I am getting strUserName null.

<authentication mode="Windows">
</authentication>

<identity impersonate="true"/>

I try to divide the line and write the code to check what is exactly returing...

 HttpContext.Current.User.Identity.Name is returning null
 System.Security.Principal.WindowsIdentity.GetCurrent().Name is returning NT AUTHORITY\IUSR when impersonate is true
 System.Security.Principal.WindowsIdentity.GetCurrent().Name is returning IIS APPPOOL\ApplicationPoolName when impersonate is false

below few link I have checked

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

User.Identity.Name with windows authentication

Community
  • 1
  • 1
Rocky
  • 4,454
  • 14
  • 64
  • 119
  • 1
    I would like to recommend you to [edit] your question and add the other similar questions you have found and tried to avoid getting your question flagged as a duplicate. – kayess Jun 13 '16 at 08:47
  • I believe that second `` tag overwrites the first one in above example so `mode=Windows` doesn't get picked up. Can you try merging both configuration items? – Aleksandar Toplek Jun 13 '16 at 09:09
  • Are you referring to a service/WCF? – Falco Alexander Jun 14 '16 at 15:24

2 Answers2

4

In Visual Studio Solution Explorer - Click on the web application project -> At the bottom select the Properties tab.Make sure you have the following set:

  1. Anonymous Authentication | Disabled
  2. Windows Authentication | Enabled

Change your Web.config like this:

  <system.web>
    <authentication mode="Windows"/>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime/>
    <pages controlRenderingCompatibilityVersion="4.0"/>
  </system.web>
  <system.webServer>
    <modules>
      <remove name="FormsAuthentication"/>
    </modules>
  </system.webServer>

Now try accessing the current Windows user like this:

string user = System.Web.HttpContext.Current.User.Identity.Name;
Denys Wessels
  • 16,829
  • 14
  • 80
  • 120
  • setting are same as you suggested, when I clean the browser history and make anonymous authentication as disable again, it started showing user name password window but it is not accepting the credentials and I am able to take remote with same credentials. It should not show any popup directly it has to check the user and redirect to default screen. – Rocky Jun 13 '16 at 10:36
  • Are you using MVC? – Denys Wessels Jun 13 '16 at 11:33
  • when I am trying to add then it is show IIS APPPOOL\ApplicationName and when I am making it true it is showing NT AUTHORITY\\IUSR, but it is not giving user name. I don't know do I need to using impersonate or not – Rocky Jun 13 '16 at 11:38
  • You need to remove all the sign in forms from your application and any logic that redirects users to the login page.You should also have a home page called Default.aspx or configure IIS to serve another page by default if yours has a different name – Denys Wessels Jun 13 '16 at 12:49
0

Try these setting in web.config

<system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" />
    <authentication mode="Windows" />
    <authorization>
      <deny users="?" />
    </authorization>
</system.web>
<system.webServer> 
</system.webServer>