MVC3:一个控制器是否需要Windows身份验证,而第二个允许匿名?

Sha*_*awn 12 authentication asp.net-mvc asp.net-mvc-3

我有一个控制器在内部Web应用程序中呈现需要进行Windows身份验证的页面.是否存在第二个控制器,用于基于JSON的查询到系统中,不需要进行Windows身份验证?那可能吗?看来我此刻只能做一个或另一个.

有什么建议?

int*_*bit 9

我们有一些应用需要做这件事.通常,我们的应用程序被锁定在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>
Run Code Online (Sandbox Code Playgroud)

但是,您仍然必须关闭该API控制器的Windows身份验证.您可以通过编辑applicationHost.configIIS服务器上的文件并添加以下内容来执行此操作:

<location path="Default Web Site/ApplicationName/ApiControllerName">
    <system.webServer>
        <security>
            <authentication>
                <anonymousAuthentication enabled="true" />
                <windowsAuthentication enabled="false" />
            </authentication>
        </security>
    </system.webServer>
</location>
Run Code Online (Sandbox Code Playgroud)

此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()
Run Code Online (Sandbox Code Playgroud)


gid*_*eon 6

是.根据您选择的身份验证,您可以使用 Authorize来装饰控制器的操作方法

本文正是您正在寻找的内容:http: //www.asp.net/mvc/tutorials/authenticating-users-with-windows-authentication-cs

从文章"例如,清单1中的Home控制器公开了三个名为Index(),CompanySecrets()和StephenSecrets()的操作.任何人都可以调用Index()操作.但是,只有Windows本地Managers组的成员才能调用CompanySecrets()动作.最后,只有名为Stephen的Windows域用户(在Redmond域中)才能调用StephenSecrets()动作."