FYI the issue is because you’re not using it over a secure connection, which you can inspect by looking at the WebServiceException thrown, e.g:
try
{
var tokenResponse = authClient.Send(new ConvertSessionToToken());
}
catch (WebServiceException ex)
{
ex.ToString().Print();
}
Which should print out:
403 Forbidden
Code: Forbidden, Message: Sending JWT over insecure connection forbidden when RequireSecureConnection=true
Server StackTrace:
[ConvertSessionToToken: 2/16/2017 4:40:20 AM]:
[REQUEST: {PreserveSession:False}]
ServiceStack.HttpError: Sending JWT over insecure connection forbidden when RequireSecureConnection=true
at ServiceStack.Auth.ConvertSessionToTokenService.Any(ConvertSessionToToken request) in C:\src\ServiceStack\src\ServiceStack\Auth\JwtAuthProvider.cs:line 279
at lambda_method(Closure , Object , Object )
at ServiceStack.Host.ServiceRunner`1.Execute(IRequest request, Object instance, TRequest requestDto) in C:\src\ServiceStack\src\ServiceStack\Host\ServiceRunner.cs:line 107
In production you’d want to ensure JWT Tokens are sent over a Secure Connection, but for testing you can disable it with:
Plugins.Add(new AuthFeature(() => new AuthUserSession(),
new IAuthProvider[]
{
new JwtAuthProvider {
HashAlgorithm = "RS256",
PrivateKeyXml = privateKeyXml,
RequireSecureConnection = false,
},
new CredentialsAuthProvider()
}));
Also note that when you use UseTokenCookie it has a similar effect to ConvertSessionToToken
where it:
removes the our Session from the App Servers Cache as now the Users Authenticated Session is contained solely in the JWT Cookie and is valid until the JWT Cookies Expiration, instead of determined by Server Session State.
If you didn’t use UseTokenCookie
, e.g:
var authClient = new JsonServiceClient(Config.ListeningOn);
var authResponse = authClient.Send(new Authenticate
{
provider = "credentials",
UserName = "Stefan",
Password = "p@55word",
RememberMe = true,
});
The JWT Token is returned in authResponse.BearerToken
and the session is left untouched, i.e. it’s still stored on the server.
But when you use ConvertSessionToToken
it embeds the current authenticated UserSession in the JWT Token Cookie:
authClient.Send(new ConvertSessionToToken());
var jwtToken = authClient.GetTokenCookie(); //From ss-tok Cookie
and deletes the Server’s UserSession so the authenticated requests are happening solely from the JWT Token.