How to enable Windows Authentication on a Windows Server 2012 IIS website using Powershell?

岁酱吖の 提交于 2019-12-01 13:49:34
sonjz

The Web.Config stated above won't need to change, those settings are still valid. The problem is, IIS itself will not obey these settings since Windows Authentication has been turned off by default at the server level.

First, ensure you have installed the Windows Authentication feature Web-Windows-Auth, and the Server Management tools -IncludeManagementTools.

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

Next, let's assume you have already handled created your site, named "AuthSite", and now I want to disable anonymous authentication and enable Windows authentication.

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" ;

NOTE: -PSPath and -Location must be used (not just the full path on -PSPath), otherwise you will encounter a locked section issue: https://stackoverflow.com/a/31416581/740575

VARIATION: Suppose you are just creating a webapp "AuthWebApp" on the "Default" site, just replace with -location "Default/AuthWebApp", -PSPath can stay the same.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!