We are using the OAuthAuthorizationServerProvider class to do authorization in our ASP.NET Web Api app.

If the provided username and password is invalid in GrantResourceOwnerCredentials, the call

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

Produces the following Json result:

{
"error": "invalid_grant",
"error_description": "The user name or password is incorrect."
}

Is there any way to customize this error result?
I would like to make it consistent with default error message format used in other parts of the API:

{
"message": "Some error occurred."
}

Is this possible to achieve with the OAuthAuthorizationServerProvider?

2016年10月12日24分24秒

This is how I did it.

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

2016年10月11日24分24秒

+1 for Dasun's answer. Here is how I extended it a bit further.

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

The .ToJsonString() is another extension method that uses the Newtonsoft.Json library.

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

Usage:

context.SetCustomError("something went wrong");

如何自定义oauthauthorizationserverprovider错误信息?的更多相关文章

  1. 自定义 ocelot 中间件输出自定义错误信息

    自定义 ocelot 中间件输出自定义错误信息 Intro ocelot 中默认的 Response 中间件在出错的时候只会设置 StatusCode 没有具体的信息,想要展示自己定义的错误信息的时候 ...

  2. jquery.validate使用 - 自定义错误信息

    自定义错误消息的显示方式 默认情况下,验证提示信息用label元素来显示, 并且会添加css class, 通过css可以很方便设置出错控件以及错误信息的显示方式. /* 输入控件验证出错*/form ...

  3. asp.net mvc3 数据验证(二)——错误信息的自定义及其本地化

    原文:asp.net mvc3 数据验证(二)--错误信息的自定义及其本地化 一.自定义错误信息         在上一篇文章中所做的验证,在界面上提示的信息都是系统自带的,有些读起来比较生硬.比如: ...

  4. SpringBoot自定义错误信息,SpringBoot适配Ajax请求

    SpringBoot自定义错误信息,SpringBoot自定义异常处理类, SpringBoot异常结果处理适配页面及Ajax请求, SpringBoot适配Ajax请求 ============== ...

  5. 自定义错误信息并写入到Elmah

    在ap.net Web项目中一直使用Elmah进行日志记录, 但一直有一个问题困扰我很久,那就是我如何自己生成一个错误并记录到Elmah里去. 你知道有时你需要在项目中生成一个错误用于一些特殊的需求 ...

  6. Spring Boot 如何自定义返回错误码错误信息

    说明 在实际的开发过程中,很多时候要定义符合自己业务的错误码和错误信息,而不是统一的而不是统一的下面这种格式返回到调用端 INTERNAL_SERVER_ERROR(500, "Intern ...

  7. springboot开发之配置自定义的错误界面和错误信息

    如何定制错误页面? (1)在有模板引擎的情况下:在template文件夹下的error/状态码:即将错误页面命名为:错误状态码.html放在template文件夹里面的error文件夹下,发生此状态码 ...

  8. strut2 自定义文件上传错误信息

    在文件上传过程中我们可以指定拦截器对文件类型.后缀名.大小进行设定,action中的配置: <interceptor-ref name="fileUpload"> &l ...

  9. Rails 自定义验证的错误信息

    Active Record 验证辅助方法的默认错误消息都是英文,为了提高用户体验,有时候我们经常会被要求按特定的文本展示错误信息.此时有两种实现方式. 1. 直接在:message添加文案 class ...

随机推荐

  1. Android 手机 ADB FastBoot 命令基本用法

    adb用法: 准备: 1.在电脑上安装相应的USB驱动,在各分区置顶帖子有下载链接 2.手机进入设置->开发人员选项->勾选USB调试 adb devices 查看是否有设备 adb sh ...

  2. 使用LM2576制作数控电源

    http://www.daxia.com/bibis/moredata30_1207792_29862.shtml 图中DA和PWM任选其一, 当DA或PWM输出为0~1.25V时,输出在12.5V~ ...

  3. mybatis如何根据mapper接口生成其实现类

    SpringBoot集成mybatis mybatis的statement的解析与加载 mybatis如何根据mapper接口生成其实现类 mybatis的mapper返回map结果集 mybatis ...

  4. mysqld 多线程 用pstree -p 显示

    http://blog.chinaunix.net/uid-22566367-id-3751084.html http://blog.csdn.net/fly2nn/article/details/6 ...

  5. ECCV 2014 Results (16 Jun, 2014) 结果已出

    Accepted Papers     Title Primary Subject Area ID 3D computer vision 93 UPnP: An optimal O(n) soluti ...

  6. MVC在基控制器中实现处理Session的逻辑

    当需要跨页面共享信息的时候,Session是首当其冲的选择,最典型的例子就是:在处理登录和购物车逻辑的时候需要用到Session.在MVC中,可以把处理Session的逻辑放在一个泛型基控制器中,但需 ...

  7. Spring事务传播机制与隔离级别(转)

    Spring事务传播机制与隔离级别 博客分类: Spring   转自:http://blog.csdn.net/edward0830ly/article/details/7569954 (写的不错) ...

  8. LR杂记-nmon+analyser监控linux系统资源

    1.查看linux具体版本号信息 file /sbin/init 2.下载相应nmon版本号 http://pkgs.repoforge.org/nmon/ 3.安装 rpm -ivh nmon-14 ...

  9. 自用广告过滤规则,整合xwhyc大大的,非常小才79K

    xwhyc大大 好久没更新了,自己弄了一点 更新: $third-party选项过滤多个站点的第三方广告:dy1000.com.yatu.tv,greasyfork.org 主流视频站点,请配合我的脚 ...

  10. 第二章 企业项目开发--maven父子模块

    2.1.maven父子模块 在实际开发中,我们基本都会用maven父子分模块的方式进行项目的开发. 2.2.实际操作 2.2.1.手工建立一个ssmm0的文件夹,并在该文件夹中加入一个pom.xml文 ...