Forms authentication: disable redirect to the login page

后端 未结 12 2537
情歌与酒
情歌与酒 2020-12-02 09:29

I have an application that uses ASP.NET Forms Authentication. For the most part, it\'s working great, but I\'m trying to add support for a simple API via an .ashx file. I wa

12条回答
  •  萌比男神i
    2020-12-02 10:15

    I know there is already an answer with tick but while trying to solve similar problem I found this (http://blog.inedo.com/2010/10/12/http-418-im-a-teapot-finally-a-%e2%80%9clegitimate%e2%80%9d-use/) as an alternative.

    Basically you return your own HTTP status code (e.g. 418) in your code. In my case a WCF data service.

    throw new DataServiceException(418, "401 Unauthorized");
    

    Then use a HTTP module to handle it at the EndRequest event to rewrite the code back to 401.

    HttpApplication app = (HttpApplication)sender;
    if (app.Context.Response.StatusCode == 418)
    {
        app.Context.Response.StatusCode = 401;
    }
    

    The browser / client will receive the correct content and status code, it works great for me :)

    If you are interested to learn more about HTTP status code 418 see this question & answer.

提交回复
热议问题