Lut*_*ndo 12 .net asp.net-authorization jwt asp.net-core asp.net-core-middleware
我们有一个调用客户端请求我们的系统没有将Bearer令牌放在标准位置('Authorization'标题)我想创建一个自定义处理程序,在正确的位置查找JWT.除了分支JwtBearerMiddleware实现是否有任何更清洁的方式我可以告诉中间件使用什么处理程序?
更简单的选择是通过在JWT中间件运行之前将JWT注入请求管道中的正确位置(请求头)来重写请求.但这似乎有点hacky.
Nat*_*ini 10
实际上有一种内置的方法,无需分叉代码或尝试提供自己的处理程序.您所要做的就是将一些代码挂钩到OnMessageReceived事件中:
app.UseJwtBearerAuthentication(new JwtBearerOptions()
{
Events = new JwtBearerEvents()
{
OnMessageReceived = context =>
{
// Get the token from some other location
// This can also await, if necessary
var token = context.Request.Headers["MyAuthHeader"];
// Set the Token property on the context to pass the token back up to the middleware
context.Token = token;
return Task.FromResult(true);
}
}
});
Run Code Online (Sandbox Code Playgroud)
如果查看源代码,Token则在执行事件处理程序后检查属性.如果它为null,则处理程序继续执行Authorization标头的默认检查.
| 归档时间: |
|
| 查看次数: |
1618 次 |
| 最近记录: |