我有一个 Controller ,用于在需要 Windows 身份验证的内部 Web 应用程序中呈现页面。是否存在第二个 Controller ,用于对系统进行基于 JSON 的查询,不需要进行 Windows 身份验证?那可能吗?看来我目前只能做其中之一。
有什么建议吗?
最佳答案
我们有一些应用程序需要执行此操作。通常,我们的应用程序被锁定在 web.config 中:
<authentication mode="Windows"/>
<authorization>
<allow roles="DOMAIN\GroupNameHere"/>
<deny users="?"/>
</authorization>
<location path="ApiControllerName">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
但是,您仍然需要关闭该 API Controller 的 Windows 身份验证。您可以通过编辑 IIS 服务器上的 applicationHost.config
文件并添加:
<location path="Default Web Site/ApplicationName/ApiControllerName">
<system.webServer>
<security>
<authentication>
<anonymousAuthentication enabled="true" />
<windowsAuthentication enabled="false" />
</authentication>
</security>
</system.webServer>
</location>
此 PowerShell 脚本将为您完成此操作:
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")
$applicationLocationPath = "Default Web Site/ApplicationName/ApiControllerName"
$oIIS = new-object Microsoft.Web.Administration.ServerManager
$oGlobalConfig = $oIIS.GetApplicationHostConfiguration()
$oSection = $oGlobalConfig.GetSection("system.webServer/security/authentication/anonymousAuthentication", $applicationLocationPath)
$oSection.SetAttributeValue("enabled", "True")
$oSection = $oGlobalConfig.GetSection("system.webServer/security/authentication/windowsAuthentication", $applicationLocationPath)
$oSection.SetAttributeValue("enabled", "False")
$oIIS.CommitChanges()
关于asp.net-mvc - MVC3 : Can one controller require Windows Authentication while a second allows anonymous?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7811617/