本文介绍了如何自定义 OAuthAuthorizationServerProvider 的错误信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用 OAuthAuthorizationServerProvider 类在我们的 ASP.NET Web Api 应用程序中进行授权.

We are using the OAuthAuthorizationServerProvider class to do authorization in our ASP.NET Web Api app.

如果GrantResourceOwnerCredentials中提供的用户名和密码无效,调用

If the provided username and password is invalid in GrantResourceOwnerCredentials, the call

context.SetError( "invalid_grant", "The user name or password is incorrect." );

产生以下 Json 结果:

Produces the following Json result:

{
    "error": "invalid_grant",
    "error_description": "The user name or password is incorrect."
}

有没有办法自定义这个错误结果?
我想让它与 API 其他部分中使用的默认错误消息格式一致:

Is there any way to customize this error result?
I would like to make it consistent with default error message format used in other parts of the API:

{
    "message": "Some error occurred."
}

这可以通过 OAuthAuthorizationServerProvider 实现吗?

Is this possible to achieve with the OAuthAuthorizationServerProvider?

推荐答案

我就是这样做的.

string jsonString = "{"message": "Some error occurred."}";

// This is just a work around to overcome an unknown internal bug.
// In future releases of Owin, you may remove this.
context.SetError(new string(' ',jsonString.Length-12));

context.Response.StatusCode = 400;
context.Response.Write(jsonString);

这篇关于如何自定义 OAuthAuthorizationServerProvider 的错误信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 11:18