I have a standalone web API project, so I had to use the Yao blog post to make a help page. Then I had to protect some of my methods from unauthorized use. I implemented this idea .
Now the fun part. I have 3 routes:
/help , which leads to the help page,
/authentication/authenticate used to invoke the authentication method and it expects user credentials and returns a security token if successful
and /transaction/{action}/{id} this route should be protected from unauthorized use.
Basically, I need to do all the routes where controller = transaction is handled by the TokenInspector .
1. Scenario: if I have such a routing configuration:
_config.Routes.MapHttpRoute( name: "AuthenticatedOnly", routeTemplate: "transaction/{action}/{id}", defaults: new {controller = "Transaction", action="GetNewTaskId", id=RouteParameter.Optional}, constraints: null, handler: tokenInspector ); _config.Routes.MapHttpRoute( "Default", "{controller}/{action}/{id}", defaults: new { controller="Help", action="Index", id = RouteParameter.Optional} );
Everything works fine except for the help page only the POST Authentication/Authenticate entry
2. Scenario: if I change the routing configuration to:
_config.Routes.MapHttpRoute( name: "AuthenticatedOnly", routeTemplate: "transaction/{action}/{id}", defaults: new {}, constraints: null, handler: tokenInspector ); _config.Routes.MapHttpRoute( "Default", "{controller}/{action}/{id}", defaults: new { controller="Help", action="Index", id = RouteParameter.Optional} );
The help page works fine and shows all the methods, but /transaction no longer protected and works without a token.
3. Scenario:
_config.Routes.MapHttpRoute( name: "AuthenticatedOnly", routeTemplate: "transaction/{action}/{id}", defaults: new {id=RouteParameter.Optional}, constraints: null, handler: tokenInspector ); _config.Routes.MapHttpRoute( "Default", "{controller}/{action}/{id}", defaults: new { controller="Help", action="Index", id = RouteParameter.Optional} );
It works with both authentication and the man page, but when I make a request like /Transaction/GetNewTaskId with a valid token in the header, I get 404.
Update Can anyone explain how the creation of the help page depends on the registered routes? Is there a way to configure it and force ApiExplorer to print the contents of the controller?
Update 2 After several attempts and investigations, I found a solution that fits my goal - to keep the documentation, as well as the security template. I implemented a special message handler (basically, I used my TokenInspector, but added a logical URL filter for it).
So now I have one route:
_config.Routes.MapHttpRoute( name: "Default", routeTemplate: "{controller}/{action}/{id}", defaults: new { controller = "Help", action = "Index", id=RouteParameter.Optional } );
and this is how I start the server:
_config = new ExtendedHttpSelfHostConfiguration(ServiceAddress); TokenInspector tokenInspector = new TokenInspector() { InnerHandler = new HttpRoutingDispatcher(_config) }; _server = new HttpSelfHostServer(_config, tokenInspector); ConfigureHost(_config); _server.OpenAsync();
Perhaps the question is as it is, such an answer cannot be answered, but in any case, thank you all for your efforts!
Regards, insomnium _