Ger*_*ald 13 oauth-2.0 jwt identityserver3 asp.net-core
所以我有一个MVC6应用程序,其中包括一个身份服务器(使用ThinkTecture的IdentityServer3)和一个MVC6 Web服务应用程序.
在Web服务应用程序中,我在启动时使用此代码:
app.UseOAuthBearerAuthentication(options =>
{
options.Authority = "http://localhost:6418/identity";
options.AutomaticAuthentication = true;
options.Audience = "http://localhost:6418/identity/resources";
});
Run Code Online (Sandbox Code Playgroud)
然后我有一个控制器,其动作具有该Authorize属性.
我有一个JavaScript应用程序,它使用身份服务器进行身份验证,然后使用提供的JWT令牌来访问Web服务操作.
这有效,我只能使用有效令牌访问该操作.
当JWT到期时问题就出现了.我得到的似乎是一个详细的ASP.NET 500错误页面,它返回以下异常的异常信息:
System.IdentityModel.Tokens.SecurityTokenExpiredException IDX10223:生命周期验证失败.令牌已过期.
我对OAuth很新,并且通常保护Web API,所以我可能会偏离基础,但对于过期令牌而言,500错误对我来说似乎不合适.对于Web服务客户端来说,它绝对不友好.
这是预期的行为,如果不是,我需要做些什么来获得更合适的回应?
Kév*_*let 11
编辑:此错误已在ASP.NET Core RC2中修复,并且不再需要此答案中描述的解决方法.
注意:由于此其他错误,此解决方法将无法在ASP.NET 5 RC1上运行.您可以迁移到RC2每晚构建或创建一个自定义中间件,捕获JWT承载中间件抛出的异常并返回401响应:
app.Use(next => async context => {
try {
await next(context);
}
catch {
// If the headers have already been sent, you can't replace the status code.
// In this case, throw an exception to close the connection.
if (context.Response.HasStarted) {
throw;
}
context.Response.StatusCode = 401;
}
});
Run Code Online (Sandbox Code Playgroud)
遗憾的是,这就是JWT/OAuth2承载中间件(由MSFT管理)目前默认工作的方式,但应该最终修复.您可以查看此GitHub票证以获取更多信息:https://github.com/aspnet/Security/issues/411
幸运的是,您可以通过使用AuthenticationFailed通知"轻松"解决这个问题:
app.UseOAuthBearerAuthentication(options => {
options.Notifications = new OAuthBearerAuthenticationNotifications {
AuthenticationFailed = notification => {
notification.HandleResponse();
return Task.FromResult<object>(null);
}
};
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2305 次 |
| 最近记录: |