OWIN OAuthAuthorizationServerProvider 自定义错误响应
OWIN OAuthAuthorizationServerProvider custom error response
对于当前的 WebAPI,我使用 OAuthAuthorizationServerProvider
的自定义实现。
如果登录失败,我需要 return 一个 JSON 响应。此响应是一个简单的 ViewModel
,包含锁定时间等。
但是,我无法发送回复。它要么在 25
个字符 (??) 处被截断,要么将其作为转义的 C# string
("\"lockout": 2"
等)
发送
这是我目前尝试过的方法:
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
context.Response.ContentType = "application/json; charset=utf-8";
context.Response.Write(JsonConvert.SerializeObject(authResult));
通过将内容长度存储在请求上下文中(暂时)解决了这个问题。
context.Request.Set("Content-Length", json.Length.ToString());
然后在Startup.cs
中获取
if (c.Request.Environment.ContainsKey("Content-Length"))
{
c.Response.ContentLength = Convert.ToInt64(c.Request.Environment["Content-Length"]);
c.Response.ContentType = "application/json; charset=utf-8";
c.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
}
对于当前的 WebAPI,我使用 OAuthAuthorizationServerProvider
的自定义实现。
如果登录失败,我需要 return 一个 JSON 响应。此响应是一个简单的 ViewModel
,包含锁定时间等。
但是,我无法发送回复。它要么在 25
个字符 (??) 处被截断,要么将其作为转义的 C# string
("\"lockout": 2"
等)
这是我目前尝试过的方法:
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
context.Response.ContentType = "application/json; charset=utf-8";
context.Response.Write(JsonConvert.SerializeObject(authResult));
通过将内容长度存储在请求上下文中(暂时)解决了这个问题。
context.Request.Set("Content-Length", json.Length.ToString());
然后在Startup.cs
中获取if (c.Request.Environment.ContainsKey("Content-Length"))
{
c.Response.ContentLength = Convert.ToInt64(c.Request.Environment["Content-Length"]);
c.Response.ContentType = "application/json; charset=utf-8";
c.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
}