20

I just upgraded my application from Visual studio 2012 to Visual studio 2013. My windows authentication is not working any more. It is giving me below error.

     HTTP Error 401.2 - Unauthorized
     You are not authorized to view this page due to invalid authentication headers.

In visual studio there is option to select authentication from website properties itself. So I disabled anonymous access and enable windows authentication but it is asking me for username and password as below popup. Even if I give domain credentials here. Its is still giving me this popup again and again.

enter image description here enter image description here

Web Config :

     <authentication mode="Windows" />
     <authorization>
     <deny users="?" />
     </authorization>
     <identity impersonate="false" />
     <trace enabled="true" />

IIS Express aspnetConfig :

     <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>

        <authorization>
            <add accessType="Allow" users="*" />
        </authorization>



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

Let me know if you want more details on this.

Update :

I found out that if I remove below line from web.config than it start working.

     <remove users="*" roles="" verbs="" />
Hiren
  • 1,381
  • 5
  • 24
  • 41

5 Answers5

24

Be careful with the applicationhost.config modifications - in Visual Studio 2015 I've found that it sometimes resides in the local project directory.

Note: The ".vs" folder is Hidden by default so you may have to select to show "Hidden Items" in Explorer to see it.

For example:

DRIVE:\MYPROJECT\.vs\config\applicationhost.config

If you're not sure which applicationhost config file is being used, you can monitor file access with ProcMon & then narrow down the results based on "Path" to see what VS is actually reading at Debug time.

Update: This appears to be the behavior in Visual Studio 2017 as well.

Coruscate5
  • 2,253
  • 17
  • 28
  • I think that the applicationhost.config is specific to the project. It's probably better to modify this file instead of the one at the IISExpress config level. – Dean Feb 25 '17 at 19:15
  • 1
    And if you use Rider, look at the `.idea` dir in your project as well.. – Maxinne May 21 '19 at 13:43
18

It looks like you solved your own question! Good on you. In addition to this post helping me I found the following to be SUPER helpful in configuring my IIS Express.

IIS Express Windows Authentication

Edit: I've copied the important information from the associated link in case it dies. This is completely from user vikomall

option-1:

edit \My Documents\IISExpress\config\applicationhost.config file and enable windowsAuthentication, i.e:

<system.webServer>
...
  <security>
...
    <authentication>
      <windowsAuthentication enabled="true" />
    </authentication>
...
  </security>
...
</system.webServer>

option-2:

Unlock windowsAuthentication section in \My Documents\IISExpress\config\applicationhost.config as follows

<add name="WindowsAuthenticationModule" lockItem="false" />

Alter override settings for the required authentication types to 'Allow'

<sectionGroup name="security">
    ...
    <sectionGroup name="system.webServer">
        ...
        <sectionGroup name="authentication">
            <section name="anonymousAuthentication" overrideModeDefault="Allow" />
            ...
            <section name="windowsAuthentication" overrideModeDefault="Allow" />
    </sectionGroup>
</sectionGroup>

Add following in the application's web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
      <security>
        <authentication>
          <windowsAuthentication enabled="true" />
        </authentication>
      </security>
    </system.webServer>
</configuration>

Below link may help: http://learn.iis.net/page.aspx/376/delegating-configuration-to-webconfig-files/

After installing VS 2010 SP1 applying option 1 + 2 may be required to get windows authentication working. In addition, you may need to set anonymous authentication to false in IIS Express applicationhost.config:

<authentication>

            <anonymousAuthentication enabled="false" userName="" />
Community
  • 1
  • 1
thinklarge
  • 672
  • 8
  • 24
5

In Visual Studio 2017, asp.net core project, the authentication is setup at launchSettings.json

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:54491/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "TestAspNetCoreProd": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "http://localhost:54492"
    }
  }
}
Huan Jiang
  • 247
  • 4
  • 13
  • These settings control how the applicationhost.config file is generated in the .vs folder. If you have already tried running the project before changing this setting, it won't appear to work, because the applicationhost.config won't be _regenerated_ automatically. You need to delete it from the disk, or manually edit the file as described by others. However, it will work better for the next guy that comes along. – Mark Aug 20 '19 at 09:56
3

For Visual Studio 2019 the applicationhost.config will be found in

DRIVE:\MYPROJECT\.vs\$(PROJECTNAME)\config\applicationhost.config

Anand
  • 717
  • 6
  • 20
1

So I just discovered, in Visual Studio 2019 anyway, clicking "properties" on a web project within a solution will allow you to disable/enable Windows authentication or Anonymous authentication. These change tags within the .csproj and .sln files for IISExpressAnonymousAuthentication and IISExpressWindowsAuthentication. This is what fixed my issue.

Eliot G
  • 11
  • 1
  • What you looking for is NOT that big project properties window with multiple tabs (i.e. application, build, web, etc.). Instead, simply click on the project and you'd see these properties in the property window (by default in the lower right under solution explorer) – sasharz Dec 07 '22 at 23:14
  • Thanks for the clarification for other users, @SashaArz. You're absolutely correct. – Eliot G Dec 28 '22 at 18:36