locked
How to customize error message of OAuthAuthorizationServerProvider in asp.net web api 2.0? RRS feed

  • Question

  • User2130689380 posted

    I have same problem like this . But when I try this code 

    public static class ContextHelper
       {
           public static void SetCustomError(this OAuthGrantResourceOwnerCredentialsContext context, string errorMessage, bool status)
           {
               var json = new ErrorMessage(errorMessage, status).ToJsonString();
     
               //context.SetError(json);            
               context.Response.Write(json);
           }
     
           public static string ToJsonString(this object obj)
           {
               return JsonConvert.SerializeObject(obj);
           }
       }




    public class ErrorMessage
          {
              public ErrorMessage(string message, bool status)
              {
                  Message = message;
                  Status = status;
              }
     
              public string Message { getprivate set; }
              public bool Status { getprivate set; }
          }


    if (user == null)
                   {
                       ContextHelper.SetCustomError(context, "Provided username and password is not matching, Please retry."false);
     
                     
                       return;
                   }





    I get "syntax error". Because text response is:

    {"access_token":null,"message":"The user name or password is incorrect"}{"error":" "}


    I can't remove {"error":" "}


    is there any way to remove {"error":" "}
    Thursday, January 30, 2020 7:45 AM

All replies