【发布时间】:2016-05-05 18:46:29
【问题描述】:
在我的 ASP.NET 应用程序中,我以这种方式覆盖了 OAuth GrantResourceOwnerCredentials:
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
using (var userManager = _userManagerFactory())
{
var user = await userManager.FindAsync(context.UserName, context.Password);
if (user == null)
{
context.Rejected();
context.SetError("invalid_grant", "Invalid username or password");
return;
}
令牌端点:
OAuthOptions = new OAuthAuthorizationServerOptions
{
AuthenticationMode = AuthenticationMode.Active,
TokenEndpointPath = new PathString("/Token"),
它有效,我收到错误客户端:
responseText: "{"error":"invalid_grant"...ame or password"}"
responseJSON: Object { error="invalid_grant", error_description="Invalid username or password"}
status: 400
statusText: "Bad Request"
现在,如果我尝试在 web.config 文件中配置自定义错误:
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="403" subStatusCode="-1"/>
<remove statusCode="404" subStatusCode="-1"/>
<remove statusCode="500" subStatusCode="-1"/>
<error statusCode="403" path="/Error/Unauthorized" responseMode="ExecuteURL"/>
<error statusCode="404" path="/Error/NotFound" responseMode="ExecuteURL"/>
<error statusCode="500" path="/Error/ServerError" responseMode="ExecuteURL"/>
</httpErrors>
我收到 Bad Request 没有 JSON 消息:
responseText:"Bad Request"
status 400
statusText:"Bad Request"
我认为自定义错误会影响所有请求并阻止预期行为。
我尝试在 web.config 中添加这个:
<location path="Token">
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<httpErrors errorMode="DetailedLocalOnly" existingResponse="PassThrough">
<clear />
</httpErrors>
</system.webServer>
</location>
它可以在我的开发者机器上使用IIS Express,但生产服务器上的IIS 8.5 似乎忽略了它。
您可以从here 下载示例项目。只需转到Login 页面并按登录按钮。我还评论了web.config 文件。
有人能指出正确的方向吗?
【问题讨论】:
-
“它工作正常,我收到错误客户端。”您期望哪个错误代码和哪个 JSON?
-
我已经更新了问题。
-
您能提供一个可验证的最小示例吗?
-
这不是 ASP.NET 身份吗?
-
我将准备一个示例项目。是的,我正在使用 asp.net 身份。