Prevent FormsAuthenticationModule of intercepting ASP.NET Web API responses

前端 未结 3 1623
一整个雨季
一整个雨季 2020-12-15 10:10

In ASP.NET the FormsAuthenticationModule intercepts any HTTP 401, and returns an HTTP 302 redirection to the login page. This is a pain for AJAX, since you ask for json and

3条回答
  •  無奈伤痛
    2020-12-15 10:17

    I was able to get around the deny anonymous setting in web.config by setting the following property:

    Request.RequestContext.HttpContext.SkipAuthorization = true;

    I do this after some checks against the Request object in the Application_BeginRequest method in Global.asax.cs, like the RawURL property and other header information to make sure the request is accessing an area that I want to allow anonymous access to. I still perform authentication/authorization once the API action is called.

提交回复
热议问题