在Powershell中为IIS应用程序启用身份验证

Jay*_*aty 22 iis powershell web-deployment

我知道如何通过以下命令为IIS网站设置此项:

Set-WebConfigurationProperty -filter "/system.webServer/security/authentication/windowsAuthentication" -name enabled -value true -PSPath "IIS:\" -location $siteName
Run Code Online (Sandbox Code Playgroud)

但我想为该网站内的应用程序设置它.例如,我有一个名为"MySite"的IIS网站,其中有两个应用程序.我想为一个启用Windows身份验证而不为另一个启用Windows身份验证.因此,对于两者都将启用站点级别的启用,这是我不想要的.

Ric*_*los 36

我遇到了处理锁定部分的问题,并且接受的答案建议打开一个GUI来解决它,我试图首先避免使用PowerShell.

简答

启用Windows身份验证并禁用匿名身份验证

$iisAppName = "MyApp"

Write-Host Disable anonymous authentication
Set-WebConfigurationProperty -Filter "/system.webServer/security/authentication/anonymousAuthentication" -Name Enabled -Value False -PSPath IIS:\ -Location "Default Web Site/$iisAppName"

Write-Host Enable windows authentication
Set-WebConfigurationProperty -Filter "/system.webServer/security/authentication/windowsAuthentication" -Name Enabled -Value True -PSPath IIS:\ -Location "Default Web Site/$iisAppName"
Run Code Online (Sandbox Code Playgroud)

处理锁定的部分

IIS文档中所述:

身份验证部分通常是锁定的,即它们不能写入web.config文件,而是必须写入中央applicationhost.config文件.

我们必须使用-PSPath-Location参数.

Set-WebConfigurationProperty -filter /system.webServer/security/authentication/windowsAuthentication -name enabled -value true -PSPath IIS:\ -location DemoSite/DemoApp
Run Code Online (Sandbox Code Playgroud)

  • 谢谢@rickglos,"处理锁定部分"解决了这个问题.其他人注意:分离PSPath和位置是必要的,以避免锁定问题! (4认同)

Jam*_*phy 23

你不需要单独-PSPath-Location参数.你可以像这样组合它们:

-PSPath "IIS:\Sites\$SiteName\$AppName"
Run Code Online (Sandbox Code Playgroud)

所以实际命令看起来像这样:

Set-WebConfigurationProperty -Filter "/system.webServer/security/authentication/windowsAuthentication" -Name Enabled -Value True -PSPath "IIS:\Sites\$SiteName\$AppName"
Run Code Online (Sandbox Code Playgroud)

请注意,您可能会遇到此错误:

Set-WebConfigurationProperty:此配置节不能在此路径中使用.当该部分被锁定在父级别时会发生这种情况.锁定是默认情况下(overrideModeDefault ="Deny"),或由locationMode ="Deny"或遗留allowOverride ="false"的位置标记显式设置.

Tomfanning了在ServerFault提供的解决方案在这里.我在这里重复了他的步骤:

  1. 打开IIS管理器
  2. 单击左侧树中的服务器名称
  3. 右侧窗格,"管理"部分,双击"配置编辑器"
  4. 在顶部,选择system.webServer/security/authentication/anonymousAuthentication部分
  5. 在右侧窗格中,单击"解锁部分"
  6. 在顶部,选择system.webServer/security/authentication/windowsAuthentication部分
  7. 在右侧窗格中,单击"解锁部分"

  • 分离-PSPath和-Location避免了锁定问题!见下面的@rickglos. (6认同)