asp.net-mvc-4 - 当整个应用程序使用Windows身份验证时,在单个 Controller 的MVC 4中使用匿名身份验证

标签 asp.net-mvc-4 windows-authentication

我有一个使用Windows身份验证的MVC4 Web应用程序,即在我拥有的web.config中
<authentication mode="Windows" /> 一切正常,一切正常。

但是,现在我需要一个 Controller (实际上是一个Web API Controller ),应该从第三方组件中进行匿名访问。问题是,每次我要调用此方法时,它都会请求用户凭据。

我尝试将AllowAnonymous属性添加到 Controller 和方法,但未成功。
[AllowAnonymous] public bool Get(string Called, string Calling, string CallID, int direction)
我在启用了匿名身份验证和Windows身份验证的IIS Express和IIS 8上都进行了检查。

看来Windows身份验证先于其他任何身份验证,并且不能被覆盖。

有没有办法做到这一点?

最佳答案

将此添加到您的Web.config。在这里,我的 Controller 名为“WebhookController”。

<location path="Webhook">
  <system.web>
    <authorization>
      <allow users="*"/>
    </authorization>
  </system.web>
</location>

See this KB article for more info.

编辑
-如下面的Erik所述,在MVC应用程序中,不应使用web.config <authorization>标记来确保安全。而是使用[Authorize]属性。这样做将使您的[AllowAnonymous]属性能够正常工作。 You can read more about this here.

关于asp.net-mvc-4 - 当整个应用程序使用Windows身份验证时,在单个 Controller 的MVC 4中使用匿名身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15087755/

相关文章:

c# - 单元测试以确保只有选定的 HTTP 动词适用于 WebAPI

c# - 将 ViewModel 传递到 CSS 文件中

asp.net - 在Windows身份验证之前拦截HTTP OPTIONS ASP.NET Core 2.0

windows-authentication - Thinktecture IdentityServer v3 与 WindowsAuth

c#-4.0 - AppSetting 值是从内存还是从 web.config 文件中读取的?

c# - ASP.NET MVC4 Web API Controller 卡在一条路线上

asp.net-mvc - 从 asp.net web api post 操作重定向

c# - 向需要 Windows 身份验证的网页发出 Web 请求

mercurial - 如何在使用 TortoiseHg 时启用 Mercurial 推/拉操作的身份验证?

session - 我在哪里可以使用 Windows 身份验证将用户信息加载到 ASP.NET MVC 5 中的 session ?