微服务:针对外部服务的C#WebAPI授权。(终点)
我有一个webapi端点。我需要使用外部jwt授权进行通话。 基本上有几种微服务。当调用命中这个特定的服务时,需要调用外部授权服务器端点来验证并获取角色和标识,该服务的标头中包含带有JWT令牌的webapi控制器 我已经通过扩展AuthorizationFilterAttribute实现了这一点。但我正在考虑使用OWIN。有没有一种方法可以通过OWIN实现这一点,即在请求到达控制器时拦截、验证令牌 注意:如果令牌无效,我将不得不取消授权 添加代码:微服务:针对外部服务的C#WebAPI授权。(终点),c#,.net,asp.net-web-api2,owin,webapi,C#,.net,Asp.net Web Api2,Owin,Webapi,我有一个webapi端点。我需要使用外部jwt授权进行通话。 基本上有几种微服务。当调用命中这个特定的服务时,需要调用外部授权服务器端点来验证并获取角色和标识,该服务的标头中包含带有JWT令牌的webapi控制器 我已经通过扩展AuthorizationFilterAttribute实现了这一点。但我正在考虑使用OWIN。有没有一种方法可以通过OWIN实现这一点,即在请求到达控制器时拦截、验证令牌 注意:如果令牌无效,我将不得不取消授权 添加代码: [JwtAuthorizationFilter
[JwtAuthorizationFilter]
public class abcController : ApiController
{
}
public class JwtAuthorizationFilterAttribute : AuthorizationFilterAttribute {
public override void OnAuthorization(HttpActionContext actionContext) {
try {
//Parse and get token from Header
var requestToken = FetchRequestTokenFromHeader(actionContext);
//Call(postasync) external enpoint to validate the token and get jwtdetails
var validationResponseMessage = JwtValidation(requestToken, actionContext);
//crate a generic identity and set actioncontext
ParseValidationResponseMessage(actionContext, validationResponseMessage);
} catch (Exception exception) {
Logger.Info("Exception on authorization: " + exception.Message);
actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.InternalServerError);
}
}
我们想知道你方做了些什么。您是否可以共享任何代码段。已添加。请检查