我将我的Web.Api项目配置为使用OAuth身份验证机制:
...
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new ApplicationOAuthProvider(PublicClientId, UserManagerFactory),
AuthorizeEndpointPath = new PathString("/Account/ExternalLogin"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
AllowInsecureHttp = true
};
...
因此它完全通过以下网址处理我的身份验证过程:http://mysite/token
如果我传递了错误的用户名/密码组合,它会给我以下json响应(使用Status Code:400 Bad Request
):
{"error":"invalid_grant",
"error_description":"The user name or password is incorrect."}
我想知道: 1-有没有办法本地化此消息? 2-有没有办法在其响应中添加自定义标题?
答案 0 :(得分:3)
首先,您应该覆盖GrantResourceOwnerCredentials函数。在该功能中,您可以添加标题或在必要时设置错误文本。
Public Overrides Function GrantResourceOwnerCredentials(context As OAuthGrantResourceOwnerCredentialsContext) As Task
.............
context.Response.Headers.Add("X-Challenge", {result.Challenge})
context.SetError("invalid challenge response")
..............
End Function