【问题标题】:Return custom "invalid grant" error message from GrantResourceOwnerCredentials从 GrantResourceOwnerCredentials 返回自定义“无效授权”错误消息
【发布时间】:2015-05-12 05:37:52
【问题描述】:

SimpleAuthorizationServerProvider 的 GrantResourceOwnerCredentials 方法中的身份验证失败时,它会返回;

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

但是,我需要使用 TEntity 类返回我自己的 JSON 响应,我们将不胜感激。

【问题讨论】:

标签: c# asp.net-web-api2 owin bearer-token


【解决方案1】:

这对你有用吗?

            context.SetError("MyCustomError", JsonConvert.SerializeObject(new removeit()
            {
                NAME = "1",
               name2 = "2",
               name3 = "3"
            }));

返回

{
    "error": "MyCustomError",
    "error_description": "{\"NAME\":\"1\",\"name2\":\"2\",\"name3\":\"3\"}"
}

【讨论】:

  • 它实际上是 Owin 的对象,具有“error”、“error_description”等属性。但是,由于身份验证失败,我需要序列化我自己的对象,并将其作为客户端的响应提供给客户端请求服务。谢谢,但不幸的是,这对我的情况没有帮助。
  • 如果你想做类似的事情: context.Response.StatusCode = (int) HttpStatusCode.Unauthorized; context.Response.ReasonPhrase = "我自己的理由";在此之上,您可以序列化您的对象并将其作为正文返回,其中包含 context.Response.Body = ?
猜你喜欢
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 2019-01-03
  • 2017-07-19
  • 1970-01-01
  • 2011-08-28
  • 2020-06-16
  • 1970-01-01
相关资源
最近更新 更多