IIS – overrideModeDefault enforced in PowerShell, not IIS UI

iisiis-7powershell

I am trying to modify an IIS Web Application that is installed, and change the Authentication Methods. The Default Web Site is set to alloy Anonymous, and disable everything else. When I use the following line in PowerShell:

Write-Host "Enabling Basic Authentication"
Set-WebConfigurationProperty -Filter /system.webServer/security/authentication/basicAuthentication -Name enabled -Value true -PSPath "IIS:\Sites\Default Web Site\$app_name"

I get the following error, and nothing changes.

Enabling Basic Authentication
Set-WebConfigurationProperty : This configuration section cannot be used at this path. This happens when the section is locked at a parent level. 
Locking is either by default (overrideModeDefault="Deny"), or set explicitly by alocation tag with overrideMode="Deny" or the legacy allowOverride="false".
At E:\web_iis_applications\web_application\ps_Install_web_application.ps1:61 char:1
+ Set-WebConfigurationProperty -Filter /system.webServer/security/authe ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Set-WebConfigurationProperty], FileLoadException
    + FullyQualifiedErrorId : System.IO.FileLoadException,Microsoft.IIs.PowerShell.Provider.SetConfigurationPropertyCommand

However, I can go right into the UI and modify those entries by a right-click and its done. This, however, is not very helpful when I have to do 20 of these installs on 2 or 3 servers. I know I shouldn't change the Default Web Site's overridgeModeDefault setting, which is why I'm trying to do it per page under default web site.

I'm not the developer of these applications, so I'm not sure if these options could be set in the web.config or not per application.

Best Answer

This releases the lock on the access and ipSecurity sections of the configuration:

$assembly = [System.Reflection.Assembly]::LoadFrom("$env:systemroot\system32\inetsrv\Microsoft.Web.Administration.dll")
$serverManager = new-object Microsoft.Web.Administration.ServerManager
$hostConfiguration = $serverManager.GetApplicationHostConfiguration()
$hostConfiguration.RootSectionGroup.SectionGroups["system.webServer"].SectionGroups['security'].Sections['access'].OverrideModeDefault = "Allow"
$hostConfiguration.RootSectionGroup.SectionGroups["system.webServer"].SectionGroups['security'].Sections['ipSecurity'].OverrideModeDefault = "Allow"
$serverManager.CommitChanges()

Now you can make calls like the following:

Set-WebConfiguration -PSPath "IIS:\sites\Default Web Site" -Filter 'system.webserver/security/access' -Value 'Ssl,SslNegotiateCert,SslRequireCert'

For example, I wanted to require Client Certificate Authentication for certain pages that were exposed externally, but put an IP filter on the rest of the "internal" pages:

Set-WebConfiguration -PSPath "IIS:\sites\Default Web Site" -Filter 'system.webserver/security/access' -Value 'Ssl,SslNegotiateCert,SslRequireCert'
Set-WebConfiguration -PSPath "IIS:\sites\Default Web Site\InternalApi" -Filter 'system.webserver/security/access' -Value 'None'
Set-WebConfiguration -PSPath "IIS:\sites\Default Web Site\InternalApi" -Filter 'system.webserver/security/ipSecurity' -Value @{allowUnlisted="False"}
Add-WebConfigurationProperty -Filter 'system.webServer/security/ipSecurity' -PSPath "IIS:\sites\Default Web Site\InternalApi" -Name "." -Value @{ipAddress="172.16.1.0";allowed="true";subnetMask="16";}