How to generate an 401 error programmatically in an ASP.NET page

后端 未结 5 1382
无人及你
无人及你 2020-12-08 02:29

As you can see this is a question from a non web developer. I would like to have an ASPX page which, under certain circumstances, can generate a 401 error from code. Ideally

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-08 03:04

    I think I still prefer:

    throw new HttpException(401, "Auth Failed")
    

    I don't think the Response.StatusCode method triggers custom errors defined in the web.config file, e.g.

    
         
         
         
    
    

    Throwing a new exception definitely triggers custom errors.

    Also, you might be using an application-wide error logging facility, like ELMAH or something, and I don't think the Response.StatusCode method would be logged there, either.

    Note: I see now the question said that, ideally, the standard IIS error page should be shown. Obviously, the custom error pages are not wanted. I would use the Response.StatusCode method in that case.

提交回复
热议问题