如何在ASP.NET Web API中自定义OAuthAuthorizationServerProvider的错误消息?

时间:2018-07-19 09:16:32

标签: asp.net-web-api oauth oauth-2.0

我遇到类似this的问题。 但是当我尝试这段代码时:

            var json = (new MyResponse { message = "The user name or password is incorrect" }).ToJsonString();
            context.SetError(new string(' ',json.Length-12)); 
            context.Response.Write(json);

-

 public class MyResponse
    {
        public string access_token { get; set; }
        public string message { get; set; }
    }

我收到“语法错误”。因为文字回应是:

  

{“ access_token”:空,“ message”:“用户名或密码为   不正确“} {”错误“:”“}

我无法删除 {“ error”:“”}

1 个答案:

答案 0 :(得分:0)

问题似乎出在 public boolean handleMessage(SOAPMessageContext context) { if((Boolean)context.get(SOAPMessageContext.MESSAGE_OUTBOUND_PROPERTY)) { SOAPMessage message = context.getMessage(); //Error is thrown from here PrintStream out = System.out; try { SOAPEnvelope envelope = message.getSOAPPart().getEnvelope(); SOAPHeader header = envelope.getHeader(); if (header == null) { header = envelope.addHeader(); } // Create Security header header =createUsernameTokenSecurityHeader(header); message.writeTo(out); } catch (Exception e) { e.printStackTrace(); } } return true; } 类或其父类可能包含名为MyResponse的属性中,该属性显示在响应中

修复以获取所需的输出以创建新的响应类

access_token

然后使用该类创建响应

public class MyNewReponse
{
  public string message {get;set;}
}