如何自定义OAuthAuthorizationServerProvider的错误消息?

时间:2014-10-13 09:14:02

标签: c# asp.net asp.net-web-api oauth

我们正在使用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来实现吗?

3 个答案:

答案 0 :(得分:8)

这就是我做到的。

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);

答案 1 :(得分:5)

+1 Dasun的回答。这是我如何进一步扩展它。

public class ErrorMessage
{
    public ErrorMessage(string message)
    {
        Message = message;
    }

    public string Message { get; private set; }
}

public static class ContextHelper
{
    public static void SetCustomError(this OAuthGrantResourceOwnerCredentialsContext context, string errorMessage)
    {
        var json = new ErrorMessage(errorMessage).ToJsonString();

        context.SetError(json);
        context.Response.Write(json);
    }
}

.ToJsonString()是另一个使用Newtonsoft.Json库的扩展方法。

public static string ToJsonString(this object obj)
    {
        return JsonConvert.SerializeObject(obj);
    }

用法:

context.SetCustomError("something went wrong");

答案 2 :(得分:0)

再次为“ user2325333”和“ Dasun's”回答他的解决方案,您的回答很好,但是仍然存在问题。 Josn标记仍返回{error:""},因此我将context.Response.Body替换为空的MemoryStream 这里是工作示例

public static class ContextHelper
{
    public static void SetCustomError(this OAuthGrantResourceOwnerCredentialsContext context,string error, string errorMessage)
    {
        var json = new ResponseMessage
        { Data = errorMessage, Message = error, IsError = true }.ToJsonString();
        context.SetError(json);
        context.Response.Write(json);
        Invoke(context);
    }
    public static string ToJsonString(this object obj)
    {
        return JsonConvert.SerializeObject(obj);
    }
    static async Task Invoke(OAuthGrantResourceOwnerCredentialsContext context)
    {
        var owinResponseStream = new MemoryStream();
        var customResponseBody = new System.Net.Http.StringContent(JsonConvert.SerializeObject(new ResponseMessage()));
        var customResponseStream = await customResponseBody.ReadAsStreamAsync();
        await customResponseStream.CopyToAsync(owinResponseStream);
        context.Response.ContentType = "application/json";
        context.Response.ContentLength = customResponseStream.Length;
        context.Response.Body = owinResponseStream;
    }
}
public class ResponseMessage
{
    public bool IsError { get; set; }
    public string Data { get; set; }
    public string Message { get; set; }
}

用于此上下文

 public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        if (!context.Match.Passcode)
        {
            context.SetCustomError("invalid_grant", "Passcode is invalid.");
            return;
        }
    }

结果将为 enter image description here

相关问题