在PowerShell中为IIS应用启用身份验证

32

我知道您可以通过以下命令为IIS网站设置:

Set-WebConfigurationProperty -filter "/system.webServer/security/authentication/windowsAuthentication" -name enabled -value true -PSPath "IIS:\" -location $siteName
但我希望将其设置为网站内的应用程序。例如,我有一个名为“MySite”的IIS网站,在其中有两个应用程序。我想为其中一个启用Windows身份验证,而另一个则不启用。因此,在站点级别启用将同时启用两者,这正是我不想要的。
2个回答

55

我遇到了处理锁定区域的问题,而被接受的答案提议通过打开GUI来解决它,但我首先想要避免使用PowerShell。

简短回答

启用Windows验证并禁用匿名验证。

$iisSiteName = "Default Web Site"
$iisAppName = "MyApp"

Write-Host Disable anonymous authentication
Set-WebConfigurationProperty -Filter '/system.webServer/security/authentication/anonymousAuthentication' -Name 'enabled' -Value 'false' -PSPath 'IIS:\' -Location "$iisSiteName/$iisAppName"

Write-Host Enable windows authentication
Set-WebConfigurationProperty -Filter '/system.webServer/security/authentication/windowsAuthentication' -Name 'enabled' -Value 'true' -PSPath 'IIS:\' -Location "$iisSiteName/$iisAppName"

处理锁定的部分

IIS文档中所述:

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

我们需要使用 -PSPath-Location 参数。

Set-WebConfigurationProperty -filter /system.webServer/security/authentication/windowsAuthentication -name enabled -value true -PSPath IIS:\ -location DemoSite/DemoApp

6
谢谢@rickglos,"处理锁定的部分"解决了问题。注意:分离PSPath和Location是必要的,以避免锁定问题! - sonjz

28

您不需要单独使用 -PSPath-Location 参数。 您可以像这样将它们组合在一起:

-PSPath "IIS:\Sites\$SiteName\$AppName"

因此,实际命令将如下所示:

Set-WebConfigurationProperty -Filter "/system.webServer/security/authentication/windowsAuthentication" -Name Enabled -Value True -PSPath "IIS:\Sites\$SiteName\$AppName"

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

Set-WebConfigurationProperty:此配置部分无法在此路径使用。 当该部分在父级别上被锁定时发生这种情况。 锁定可以是默认值(overrideModeDefault =“Deny”),也可以通过具有overrideMode =“Deny”或传统的allowOverride =“false”的位置标记显式设置。

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

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

8
将“-PSPath”和“-Location”分开使用可以避免锁定问题!请参见下面的@rickglos。 - sonjz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接