I've been following a number of sources to try and implement JWT authentication into my WebApi2 project and have also started using swagger.io and Swashbuckle to both document and test my API. I've been inspired by how exceptionless supports an /auth/login
method to generate an api_key
which is then used to call other methods requiring authorisation.
I should also say my development has been based on the ASP.NET Web API 2 book which introduces Token-Based security using JwtAuthForWebAPI
package and so I have my JWT Delegating Handler implemented for me as follows:
// Add Jwt Authentication Web Handler
var builder = new SecurityTokenBuilder();
var reader = new ConfigurationReader();
GlobalConfiguration.Configuration.MessageHandlers.Add(
new JwtAuthenticationMessageHandler
{
AllowedAudience = reader.AllowedAudience,
Issuer = reader.Issuer,
SigningToken = builder.CreateFromKey(reader.SymmetricKey),
//SigningToken = builder.CreateFromKey(reader.SubjectCertificateName),
CookieNameToCheckForToken = reader.CookieNameToCheckForToken
});
(I've set up web.config credentials and the code steps through fine)
The problem is:
- Unlike exceptionless, when I call a method after entering my
api_key
, swagger is putting the key onto the querystring instead of in a "Bearer" authorisation header - I don't know if the above code checks the querystring for the JWT (and even if it did, it doesn't sound that sensible for lots of reasons (security, long URL)
- Because the above code builds the Delegating Handler for me, I can't debug it to see what it is actually doing (can I link log4net into it?)
- And as a result, my controller method doesn't get hit because
[Authorise]
fails.
My suppose my question at this point is to resolve item 1 above which will make 2 a non-issue and (hopefully) make 4 work! Thanks.