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

发布于 2024-12-10 18:04:46 字数 134 浏览 0 评论 0原文

我有一个控制器,用于在需要 Windows 身份验证的内部 Web 应用程序中呈现页面。是否存在第二个控制器,用于对系统进行基于 JSON 的查询,不需要进行 Windows 身份验证?这可能吗?看来我目前只能做其中之一。

有什么建议吗?

I have one controller that renders pages in an internal web application that needs to be windows authenticated. There exists a second controller used for JSON-based queries into the system that do NOT need to be Windows Authenticated? Is that possible? It appears I've only been able to do one or the other at the moment.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜警司 2024-12-17 18:04:46

我们有一些应用程序需要执行此操作。通常,我们的应用程序被锁定在 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 控制器的 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()

We have a few apps that need to do this exact thing. Often, our apps are locked down in the 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>

However, you still have to turn off Windows authentication for that API Controller. You can do this by editing the applicationHost.config file on the IIS server and adding:

<location path="Default Web Site/ApplicationName/ApiControllerName">
    <system.webServer>
        <security>
            <authentication>
                <anonymousAuthentication enabled="true" />
                <windowsAuthentication enabled="false" />
            </authentication>
        </security>
    </system.webServer>
</location>

This PowerShell script will do it for you:

[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()
悲欢浪云 2024-12-17 18:04:46

是的。根据您选择的身份验证,您可以使用 授权

本文准确呈现了您正在寻找的内容:
http://www.asp.net/mvc/教程/authentiating-users-with-windows-authentication-cs

来自文章“例如,清单 1 中的 Home 控制器公开了三个名为Index()、CompanySecrets() 和 StephenSecrets()。任何人都可以调用 Index() 操作,但是,只有 Windows 本地 Managers 组的成员才能调用 CompanySecrets() 操作。在 Redmond 域中)可以调用 StephenSecrets() 操作。”

Yes. Based on what authentication you choose, you decorate your controller's action method with Authorize

This article presents exactly what you are looking for:
http://www.asp.net/mvc/tutorials/authenticating-users-with-windows-authentication-cs

From the article "For example, the Home controller in Listing 1 exposes three actions named Index(), CompanySecrets(), and StephenSecrets(). Anyone can invoke the Index() action. However, only members of the Windows local Managers group can invoke the CompanySecrets() action. Finally, only the Windows domain user named Stephen (in the Redmond domain) can invoke the StephenSecrets() action."

玉环 2024-12-17 18:04:46

,您可以使用 AuthorizeAttribute 处理此问题

是的 例如,在一个简单的帐户控制器中,您只希望授权用户访问操作 ChangePassword

   [Authorize]
    public ActionResult ChangePassword()
    {
       // your code here
    }

yes you can handle this with AuthorizeAttribute

So for example in a simple account controller you only want authorized users to access the Action ChangePassword

   [Authorize]
    public ActionResult ChangePassword()
    {
       // your code here
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文