Not sure JWT is what you want, JWT is generally used in addition with your existing Auth Providers to authenticate the User, then you can retrieve a JWT Token from your Authenticated Users Session. So it’s not a replacement for your existing Auth Providers, it’s an addition letting you use JWT’s to authenticate after you’re retrieved the JWT Token.
So my recommendation would be to get your other Auth Provider working first (e.g. CredentialsAuthProvider
), then looking at enabling JWT as we’ve done in the examples below:
The Switching Sites to use JWT shows the different ways we’ve enabled JWT on existing sites in TechStacks with a single Ajax call:
$.post("/session-to-token");
Likewise Gistlyn uses the new Fetch API to convert an existing Github OAuth into a JWT Token Cookie:
fetch("/session-to-token", { method:"POST", credentials:"include" });
For our https://servicestack.net website we just changed the normal Username/Password Credentials Auth form and added an additional UseTokenCookie
option as a hidden variable in our FORM request:
<form id="form-login" action="/auth/credentials">
<input type="hidden" name="UseTokenCookie" value="true" />
...
<input class="form-control" type="text" name="UserName" value="">
...
<input class="form-control" type="password" name="Password">
</form>
Which instead of setting up a Users Session on the Server against ServiceStack’s ss-id/ss-pid Session Cookies it will create a JWT Token and add it to the ss-tok
Cookie so each subsequent requests include the JWT Token in ss-tok
and authenticates that way.
So other than the JWT Auth Provider registration in TechStacks and in Gistlyn, the above code is the really only thing we changed to change to use JWT Authentication in our Apps.
I can’t link to the private repo containing the servicestack.net website but this is basically our AuthFeature Registration:
Plugins.Add(new AuthFeature(() => new CustomUserSession(),
new IAuthProvider[] {
new CredentialsAuthProvider(appSettings),
new JwtAuthProvider(appSettings),
}) {
IncludeRegistrationService = true,
MaxLoginAttempts = appSettings.Get("MaxLoginAttempts", 5),
});
container.Register<IUserAuthRepository>(c =>
new OrmLiteAuthRepository(c.Resolve<IDbConnectionFactory>()));
container.Resolve<IUserAuthRepository>().InitSchema();