从端到端,如何在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的配置?
注意:我会提供自我答案。
答案 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
就可以保持不变。