asp.net-mvc - MVC3 : Can one controller require Windows Authentication while a second allows anonymous?

标签 asp.net-mvc asp.net-mvc-3 authentication

我有一个 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/

相关文章:

performance - asp.net MVC3 中输出缓存的替代缓存解决方案有哪些?

java - 没有使用 Spring Security 进行身份验证和授权

jakarta-ee - 如何使用 servlet 对用户进行身份验证

javascript - 使用 RESTful Web 服务身份验证的 AngularJS

asp.net - 如何将我的模型数据(列表)与另一个 View 模型数据(列表)映射 MVC asp.net

asp.net - VS安装过程中找不到符合以下参数的产品

asp.net-mvc - Azure 和 MVC2 Web 角色模板

asp.net-mvc-3 - mvc3 https & http

c# - 从 Application_Error 抛出 http 异常(未经授权)

asp.net-mvc - 当 ModelState 无效时调用 Ajax.BeginForm OnFailure