如何自定义oauthauthorizationserverprovider错误信息?
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错误信息?的更多相关文章
- 自定义 ocelot 中间件输出自定义错误信息
自定义 ocelot 中间件输出自定义错误信息 Intro ocelot 中默认的 Response 中间件在出错的时候只会设置 StatusCode 没有具体的信息,想要展示自己定义的错误信息的时候 ...
- jquery.validate使用 - 自定义错误信息
自定义错误消息的显示方式 默认情况下,验证提示信息用label元素来显示, 并且会添加css class, 通过css可以很方便设置出错控件以及错误信息的显示方式. /* 输入控件验证出错*/form ...
- asp.net mvc3 数据验证(二)——错误信息的自定义及其本地化
原文:asp.net mvc3 数据验证(二)--错误信息的自定义及其本地化 一.自定义错误信息 在上一篇文章中所做的验证,在界面上提示的信息都是系统自带的,有些读起来比较生硬.比如: ...
- SpringBoot自定义错误信息,SpringBoot适配Ajax请求
SpringBoot自定义错误信息,SpringBoot自定义异常处理类, SpringBoot异常结果处理适配页面及Ajax请求, SpringBoot适配Ajax请求 ============== ...
- 自定义错误信息并写入到Elmah
在ap.net Web项目中一直使用Elmah进行日志记录, 但一直有一个问题困扰我很久,那就是我如何自己生成一个错误并记录到Elmah里去. 你知道有时你需要在项目中生成一个错误用于一些特殊的需求 ...
- Spring Boot 如何自定义返回错误码错误信息
说明 在实际的开发过程中,很多时候要定义符合自己业务的错误码和错误信息,而不是统一的而不是统一的下面这种格式返回到调用端 INTERNAL_SERVER_ERROR(500, "Intern ...
- springboot开发之配置自定义的错误界面和错误信息
如何定制错误页面? (1)在有模板引擎的情况下:在template文件夹下的error/状态码:即将错误页面命名为:错误状态码.html放在template文件夹里面的error文件夹下,发生此状态码 ...
- strut2 自定义文件上传错误信息
在文件上传过程中我们可以指定拦截器对文件类型.后缀名.大小进行设定,action中的配置: <interceptor-ref name="fileUpload"> &l ...
- Rails 自定义验证的错误信息
Active Record 验证辅助方法的默认错误消息都是英文,为了提高用户体验,有时候我们经常会被要求按特定的文本展示错误信息.此时有两种实现方式. 1. 直接在:message添加文案 class ...
随机推荐
- Redhat Enterprise Linux 7.4/CentOS 7.4 安装后初始化配置
由于我是最小化安装,需要在安装后进行一些配置 1. 设定启动级别 [root@home ~]# systemctl set-default multi-user.target 2. 设定网络 [roo ...
- VS增强插件 Supercharger破解教程
Supercharger 破解教程:步骤:1.打开Supercharger的options; 2.点击Pricing & Registration 3.复制 license tt4e2HN4X ...
- log4j用properties文件配置,无法应用过滤器的解决方法
properties文件仍然有错误 log4j:WARN Failed to set property [filter] to value "org.apache.log4j.varia.L ...
- python笔记30-docstring注释添加变量
前言 python里面添加字符串注释非常简单,如何将变量放入 python 的函数注释里面呢? docstring也就是给代码加注释的内容了,python可以给函数,类.方法,模块添加注释内容,注释标 ...
- POJ--2570--Fiber Network【floyd+位运算】
题意:一些公司决定搭建一些光纤网络.单向的,假设从第一点到第二点,有ab两个公司能够搭建,第二点到第三点有ac两个公司能够搭建,第一点到第三点有d公司能够搭建,则第一点到第三点有a.d两个公司能够搭建 ...
- acd Convex(求面积)
Problem Description We have a special convex that all points have the same distance to origin point. ...
- C++类模板的声明和定义为什么要放在同一个文件
不是只能放在.h里面,但是推荐放在.h里面.STL模板实现全部是放在.h里面的.------------------编译能通过.1)参与编译的只是.cpp文件,不会报错的原因,是因为它能在.h里面找到 ...
- 第六章 JVM垃圾收集器(2)
上一章记录了几种常见的垃圾收集器,见<第五章 JVM垃圾收集器(1)> 1.G1 说明: 从上图来看,G1与CMS相比,仅在最后的"筛选回收"部分不同(CMS是并发清除 ...
- 如何关掉UAC?
你可能会问, 这也值得一写? 唉, 怎么说呢, 谁让咱一开始不会呢. ^_^ 好记性不如烂笔头嘛. 1. 打开一个command prompt, 输入msconfig回车. 2. 点击tools选 ...
- Anagrams leetcode java
题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will ...