2013-12-23 96 views

回答

2

我结束了创建两个MessageHandlers。一个做基本身份验证,另一个做基于令牌的身份验证。然后,我设置如下所示的按路由消息处理:

public static void Register(HttpConfiguration config) 
    { 
     // Web API configuration and services 

     // Web API routes 
     config.MapHttpAttributeRoutes(); 

     config.Routes.MapHttpRoute(
      name: "BasicAuthApi", 
      routeTemplate: "api/{controller}/{id}", 
      defaults: new { id = RouteParameter.Optional }, 
      constraints: new { controller = "controller1" }, 
      handler: new BasicAuthMessageHandler() { InnerHandler = new HttpControllerDispatcher(config) } 
     ); 

     config.Routes.MapHttpRoute(
      name: "TokenAuthApi", 
      routeTemplate: "api/{controller}/{id}", 
      defaults: new { id = RouteParameter.Optional }, 
      constraints: new { controller = "controller2" }, 
      handler: new TokenAuthMessageHandler() { InnerHandler = new HttpControllerDispatcher(config) } 
     ); 
    } 
+0

这在我看来是更好的解决方案,而不是使用过滤器。我也这样做。我为每个路由设置了处理程序。验证应该在处理程序而不是过滤器中完成。 – HelloWorld

3

看一看在网页API v2的新身份验证筛选器。它们专门用于使用身份验证方法注释控制器或操作。

您可以直接在控制器中实施身份验证方法 - 或者使用OWIN/Katana回拨身份验证中间件。

看到这里的基本身份验证的中间件: https://github.com/thinktecture/Thinktecture.IdentityModel/tree/master/source/Thinktecture.IdentityModel.Owin/Basic%20Authentication

+1

我删除了基于使用'ClaimsAuthorizationManager'查看身份验证类型的答案。我想使用认证过滤器是一个更清洁的解决方案。 – Badri

+0

感谢您指出正确的方向。我最终使用消息处理程序而不是过滤器。 – Pradeep