【发布时间】:2016-08-02 05:45:00
【问题描述】:
如何禁用 Web API 中特定 GET 处理程序的授权过滤器?
在类级别有一个自定义授权过滤器,但对于其中一种方法,我需要没有安全性。我尝试应用 [AllowAnonymous] 属性,但它仍然通过更高级别的过滤器并失败。该自定义过滤器派生自AuthorizationFilterAttribute。该类还有另外两个属性:OverrideAuthentication 和 EnableCors。
我尝试了AllowAnonymous 属性,但没有。
示例代码:
[EnableCors(origins: "*", headers: "*", methods: "*")]
[OverrideAuthentication]
[AccountAuthorization]
public class AccountsController : ApiController
{
[Route("api/accounts/{accountNumber}/GetX")]
[AllowAnonymous]
[HttpGet]
public HttpResponseMessage GetX(string accountNumber)
{
HttpResponseMessage response = null;
IEnumerable<string> apiKey;
if (!Request.Headers.TryGetValues("X-ApiKey", out apiKey) || apiKey.Count() != 1 || apiKey.First() != API_KEY)
{
throw new HttpResponseException(HttpStatusCode.Forbidden);
}
// Process
// ..
// ..
return response;
}
}
编辑:链接的答案没有解释解决方案是什么。
【问题讨论】:
-
如果从控制器中删除
[OverrideAuthentication]和[AccountAuthorization]会怎样? -
也看到这个,你也许可以在 web api 中的特定 URL 上允许匿名..stackoverflow.com/questions/14588397/…
-
@MaxSorin:还是不行。
-
那你在其他地方截取我们不可见的请求。
标签: c# asp.net authorization asp.net-web-api2 authorize-attribute