如何使用Powershell在Windows Server 2012 IIS网站上启用Windows身份验证?

时间:2016-04-11 16:19:31

标签: asp.net powershell iis windows-server-2012

从端到端,如何在Windows Server 2012中的ASP.NET应用程序上设置Windows身份验证?

在早期版本的IIS中,您只需在<authentication>中设置<identity><authorization>Web.Config设置即可完成。

<!-- Web.Config -->
<system.web>
  ...
  <authentication mode="Windows />
  <identity impersonate="false />
  <authorization>
    <allow users="DOMAIN\user1" />
    <allow users="DOMAIN\user2" />
    <deny users="*" />
  </authorization>

现在有一个额外的安全组件,要求您在IIS站点/ webapp本身上启用身份验证。

我为我们的Window Server 2012网络服务器编写了一个引导脚本,如何在Powershell中完成IIS的配置?

注意:我会提供自我答案。

1 个答案:

答案 0 :(得分:1)

上述Web.Config不需要更改,这些设置仍然有效。问题是,IIS本身不会遵守这些设置,因为Windows身份验证在服务器级别默认关闭。

首先,确保您已安装Windows身份验证功能Web-Windows-Auth和服务器管理工​​具-IncludeManagementTools

Install-WindowsFeature "Web-Windows-Auth" -IncludeManagementTools ; 

接下来,让我们假设您已经处理创建了您的网站,名为&#34; AuthSite&#34;,现在我想禁用匿名身份验证并启用Windows身份验证。

Import-Module WebAdministration ;

# disable anonymous
Set-WebConfigurationProperty `
  -filter "/system.webserver/security/authentication/anonymousAuthentication" `
  -PSPath "IIS:\" `
  -location "AuthSite" `
  -name "enabled" `
  -value "False" ;

# enable Windows authentication
Set-WebConfigurationProperty `
  -filter "/system.webserver/security/authentication/windowsAuthentication" `
  -PSPath "IIS:\" `
  -location "AuthSite" `
  -name "enabled" `
  -value "True" ;

注意:必须使用-PSPath-Location(而不仅仅是-PSPath上的完整路径),否则您会遇到锁定的部分问题:https://stackoverflow.com/a/31416581/740575

变化:假设您只是创建一个Web应用程序&#34; AuthWebApp&#34;在&#34;默认&#34;网站,只需替换为-location "Default/AuthWebApp"-PSPath就可以保持不变。

相关问题