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