我们正在使用 OAuthAuthorizationServerProvider
类在我们的 ASP.NET Web Api 应用程序中进行授权。
如果在 GrantResourceOwnerCredentials
中提供的用户名和密码无效,调用
context.SetError( "invalid_grant", "The user name or password is incorrect." );
生成以下 Json 结果:
{
"error": "invalid_grant",
"error_description": "The user name or password is incorrect."
}
有没有办法自定义这个错误结果?
我想让它与 API 其他部分使用的默认错误消息格式一致:
{
"message": "Some error occurred."
}
这可以通过 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);
关于c# - 如何自定义OAuthAuthorizationServerProvider的报错信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26336481/