我无法根据路由执行 WebAPI DelegatingHandler
public class JwtCookieHandler : DelegatingHandler
{
public static string SessionIdToken = "session-id";
async protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
//blah blah////////////////
HttpResponseMessage response = await base.SendAsync(request,
cancellationToken);
// Set the session ID as a cookie in the response message.
response.Headers.AddCookies(new CookieHeaderValue[] {
new CookieHeaderValue(SessionIdToken, sessionId) });
return response;
}
关于 Web api 注册:
public static void Register(HttpConfiguration config)
{
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "SecureApi",
routeTemplate: "secure/{controller}/{id}",
defaults: new { id = RouteParameter.Optional },
constraints: null,
handler: new JwtCookieHandler()
);
}
在 Web API 上:
public class DataController : ApiController
{
[Route("secure/data/abc")]
[HttpGet]
public HttpResponseMessage ABC()
{
return new HttpResponseMessage()
{
Content = new StringContent("hello");
};
}
}
如果我调用https://example.com/secure/data/abc => 它会显示 api 数据但从不调用 JwtCookieHandler。我做错了什么 ?
如果我将其设置为全局工作
config.MessageHandlers.Add(new JwtCookieHandler()); => invoke the handler for every api call