106

I'm trying to pick up the windows username when debugging in Visual Studio 2013. I am simply using:

httpcontext.current.user.identity.name

If I run this on my Dev Server it works fine, if I run it in debug mode on any previous version of Visual Studio it also works fine.

My problems is - If i run this on visual studio 2013 I get an empty string.

My web config is as follows.

<system.web>
    <authentication mode="Windows"/>
    <identity impersonate="false"/>
    <authorization>
       <allow users="*"/>
    </authorization>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.0"/>
    <customErrors mode="Off"/>
</system.web>

9 Answers 9

217

I had just upgraded to VS 2013 from VS 2012 and the current user identity (HttpContext.User.Identity) was coming through as anonymous.

I tried changing the IIS express applicationhost.config, no difference.

The solution was to look at the properties of the web project, hit F4 to get the project properties when you have the top level of the project selected. Do not right click on the project and select properties, this is something entirely different.

Change Anonymous Authentication to be Disabled and Windows Authentication to be Enabled.

Works like gravy :)

9
  • 10
    Thanks for that one. I cannot believe they have added that as an 'enhancement'. That is what web.config is for. Now we can't trust what we see in web.config. Brilliant. Commented Feb 28, 2014 at 12:04
  • 4
    I believe this is a better approach than the accepted answer, as it wouldn't affect other application running on the IIS Express.
    – g45rg34d
    Commented Apr 18, 2014 at 8:26
  • Thanks a bunch. That did it for me as well! Was a Visual Studio 2008 Project I upgraded to Visual Studio 2013.
    – Rob K.
    Commented Jul 28, 2014 at 19:38
  • In VS 2012 I used: "Use Visual Studio Development Server". So an upgrade and this worked. Even though it now runs IIS Express. Commented Sep 22, 2014 at 13:42
  • This one should be the accepted answer, if you've just upgraded to VS 2013.
    – Nishant
    Commented Dec 15, 2014 at 21:58
130

As I was researching this I found my answer, but can't find the answer on the internet, so I thought I'd share this:

I fixed my issue by modifying my applicationhost.config file. My file was saved in the "\My Documents\IISExpress\config" folder.

It seems that VS2013 was ignoring my web.config file and applying different authentication methods.

I had to modify this portion of the file to look like the below. In truth, I only modified the anonymousAuthentication to be false and the windowsAuthentication mode to true.

<authentication>

  <anonymousAuthentication enabled="false" userName="" />

  <basicAuthentication enabled="false" />

  <clientCertificateMappingAuthentication enabled="false" />

  <digestAuthentication enabled="false" />

  <iisClientCertificateMappingAuthentication enabled="false">
  </iisClientCertificateMappingAuthentication>

  <windowsAuthentication enabled="true">
    <providers>
      <add value="Negotiate" />
      <add value="NTLM" />
    </providers>
  </windowsAuthentication>

</authentication>
9
  • 2
    I had to do this to get debugging in VS2013 using IIS Express to work reliably. Without this, seemed to work OK once, then 401 thereafter. (1) Is there an AppCmd command that would change the applicationhost.config file, and (2), F4 on the Web Project lets me switch Anonymous Auth off and Windows Auth on. Doing that in VS2012 worked fine, in VS2013 does not produce a 401 free dev environment.
    – IanT8
    Commented Nov 4, 2013 at 1:27
  • 4
    @Neil, you saved my day. BTW: It is not necessary to turn off anonymousAuthentication. It is sufficient to turn on windowsAuthentication. These settings control what authentication mechanisms the web sites are allowed to use.
    – chiccodoro
    Commented Feb 11, 2014 at 16:22
  • +1 as well...been trying to solve this nagging issue for a while with my local IISExpress isntance
    – Jeff Lewis
    Commented May 30, 2014 at 20:23
  • Ah, THAT's how to do it. Genius. Thanks for this tip, really useful ! Commented Aug 15, 2014 at 9:14
  • 1
    I had to remove the "Negotiate" to be able to test a webservice call (with WSE3) without getting a 401.
    – Wolf5
    Commented Dec 2, 2014 at 17:11
41

In Visual Studio 2013 AND VS15 (but i guess if the same for all other version) just press F4 and change this two properties: -Anonymous Authentication: Disable -Windows Authentication: Enable

3
  • 1
    Thanks for that one. I cannot believe they have added that as an 'enhancement'. That is what web.config is for. Now we can't trust what we see in web.config. Brilliant. Commented Feb 28, 2014 at 12:03
  • 1
    You just saved me from a rollback to Visual Studio 2010!
    – Julien P
    Commented May 15, 2014 at 9:02
  • 2
    Cant believe this was not #1 questio/answer out of all these SO posts those keep on talking about IIS settings
    – Lost
    Commented Oct 23, 2015 at 17:03
38

In VS2013 F4 on your project to view properties window and disable Anonymous access and enable "Windows authentication"

Then it will work. No need to change anything else

1
  • Frustratingly, this option exists when you want to debug a web project, but doesn't exist for Service projects. Grrrr... Commented Nov 14, 2014 at 11:16
9

VS 2015 changes this. It added a .vs folder to my web project and the applicationhost.config was in there. I made the changes suggested (window authentication = true, anon=false) and it started delivering a username instead of a blank.

0
8

Open up the applicationHost.config file located in the C:\Users[userid]\Documents\IISExpress\config folder. Inside this file change the overrideModeDefault of anonymousAthentication and windowsAuthentication to "Allow"

 <sectionGroup name="security">
                <section name="access" overrideModeDefault="Deny" />
                <section name="applicationDependencies" overrideModeDefault="Deny" />
                <sectionGroup name="authentication">
                    <section name="anonymousAuthentication" overrideModeDefault="Allow" />
                    <section name="basicAuthentication" overrideModeDefault="Deny" />
                    <section name="clientCertificateMappingAuthentication" overrideModeDefault="Deny" />
                    <section name="digestAuthentication" overrideModeDefault="Deny" />
                    <section name="iisClientCertificateMappingAuthentication" overrideModeDefault="Deny" />
                    <section name="windowsAuthentication" overrideModeDefault="Allow" />
                </sectionGroup>

Next change lockItem to be "false" for AnonymousAuthenticationModule and WindowsAuthenticationModule

  <system.webServer>
            <modules>
                <!--
                <add name="HttpCacheModule" lockItem="true" />
-->
                <add name="DynamicCompressionModule" lockItem="true" />
                <add name="StaticCompressionModule" lockItem="true" />
                <add name="DefaultDocumentModule" lockItem="true" />
                <add name="DirectoryListingModule" lockItem="true" />
                <add name="IsapiFilterModule" lockItem="true" />
                <add name="ProtocolSupportModule" lockItem="true" />
                <add name="HttpRedirectionModule" lockItem="true" />
                <add name="ServerSideIncludeModule" lockItem="true" />
                <add name="StaticFileModule" lockItem="true" />
                <add name="AnonymousAuthenticationModule" lockItem="false" />
                <add name="CertificateMappingAuthenticationModule" lockItem="true" />
                <add name="UrlAuthorizationModule" lockItem="true" />
                <add name="BasicAuthenticationModule" lockItem="true" />
                <add name="WindowsAuthenticationModule" lockItem="false" />

Making these changes will allow the existing web config settings to override what is in the applicationHost file for IIS Express.

1
  • 1
    These changes plus a logoff worked for me. Something is cached, because stopping IIS Express from the notification icon and restarting VS doesn't work. This error is really annoying, it occurs every few months or half year when a new VM or developer PC is built. It always causes up to half a day lost, messing around with settings. The problem is although these settings should fix it immediately, it doesn't and after playing around it suddenly works, very strange and I don't like that. A logoff is then recommended. Next time it happens I'll try to isolate, but this is a very annoying default.
    – Tony Wall
    Commented Mar 31, 2016 at 9:24
6

You could also modify the project properties for your web project, choose "Web" from left tabs, then change the Servers drop down to "Local IIS". Create a new virtual directory and use IIS manager to setup your site/app pool as desired.

I prefer this method, as you would typically have a local IIS v-directory (or site) to test locally. You won't affect any other sites this way either.

Web Project Properties

2

F4 doesn't always bring me to this panel. Besides, it is often said that a picture is worth a thousand words.

enter image description here

1

It appears that the right answer is provided by user3149240 above. However, As Neil Watson pointed out, the applicationhost.config file is at play here.

The changes can actually be made in the VS Property pane or in the file albeit in a different spot. Near the bottom of the applicationhost.config file is a set of location elements. Each app for IIS Express seems to have one of these. Changing the settings in the UI updates this section of the file. So, you can either change the settings through the UI or modify this file.

Here is an example with anonymous auth off and Windows auth on:

<location path="MyApp">
    <system.webServer>
        <security>
            <authentication>
                <windowsAuthentication enabled="true" />
                <anonymousAuthentication enabled="false" />
            </authentication>
        </security>
    </system.webServer>
</location>

This is equivalent in the VS UI to:

Anonymous Authentication: Disabled
Windows Authentication: Enabled
1
  • In the current VS this does not work, it will always say it is locked even when the applicationhost.config modules have been unlocked in both user documents and solution .vs/config paths. Also it is not true this is the only place, the settings from the properties window are actually stored in the VS web project's XML. But it doesn't matter because changing them also doesn't help, the applicationhost always overrides. A logoff was necessary to get those changes to work, that's all.
    – Tony Wall
    Commented Mar 31, 2016 at 9:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.