在我的WebAPI项目中,我有一些装饰有[Authorize]
属性的apis。如果在WebAPI中授权失败,如何返回自定义消息
[Authorize]
public HttpResponseMessage GetCustomers()
{
//my api
}
如果用户没有正确的令牌,则拒绝访问异常返回给用户。
但我需要的是,在任何这种情况下,我需要返回自定义响应消息。
{
"StatusCode" : 403,
"message": "You donot have sufficient permission"
}
如何在授权失败的情况下返回此自定义消息。
请注意:
- 我使用Owin - 基于令牌的认证。
- 我是不是将访问令牌存储在我的数据库或其他任何地方。
已阅读这里的答案:http://stackoverflow.com/questions/20149750/owin-unauthorised-webapi-call-returning-login-page-rather-than-401 –
http://stackoverflow.com/questions/2578756/how-to-make-authorize-attribute-return-custom-403-error-page-instead-of-redirect –