First time everything is Ok, once the first project restarted and the second project is not reopen, the winform will called OnAuthenticationRequired, but It return client.BearerToken is null. If I started the winform again, It works fine.
In Servicestack 4.5.6 all is good, after upgrade 4.5.8, It has error. Is it need to use RefreshToken?
If I changed the GetNewClient to the code , It works fine. even if the website is restart. But I Have some old project is based on .net framework 4.03, can’t upgrade to .net framework 4.5, there has no RefreshToken, how to solve it ?
protected virtual IJsonServiceClient GetClient() => new JsonServiceClient(f.ApiUrl);
private string GetRefreshToken()
{
var authClient = GetClient();
var refreshToken = authClient.Send(new Authenticate
{
provider = "credentials",
UserName = userName,
Password = password,
}).RefreshToken;
return refreshToken;
}
public JsonServiceClient GetNewClient()
{
var client = GetClient();
var serviceClient = client as JsonServiceClient;
if (serviceClient == null) //OnAuthenticationRequired not implemented in JsonHttpClient
return null;
serviceClient.OnAuthenticationRequired = () =>
{
var authClient = GetClient();
serviceClient.BearerToken = authClient.Send(new GetAccessToken
{
RefreshToken = GetRefreshToken(),
}).AccessToken;
};
return serviceClient;
}
Not sure what the question is, JsonHttpClient doesn’t implement OnAuthenticationRequired but it does implement the new Refresh Tokens support which you can use instead.
[Test]
public void Can_Auto_reconnect_with_BasicAuth_after_expired_token()
{
var authClient = GetClientWithUserPassword(alwaysSend: true);
var called = 0;
var client = new JsonServiceClient(ListeningOn)
{
BearerToken = CreateExpiredToken(),
};
client.OnAuthenticationRequired = () =>
{
called++;
client.BearerToken = authClient.Send(new Authenticate()).BearerToken;
};
var request = new Secured { Name = "test" };
var response = client.Send(request);
Assert.That(response.Result, Is.EqualTo(request.Name));
response = client.Send(request);
Assert.That(response.Result, Is.EqualTo(request.Name));
Assert.That(called, Is.EqualTo(1));
}
in the source file JwtAuthProviderTests.cs has
[Test]
public void Only_returns_Tokens_on_Requests_that_Authenticate_the_user()
{
var authClient = GetClient();
var refreshToken = authClient.Send(new Authenticate
{
provider = "credentials",
UserName = Username,
Password = Password,
}).RefreshToken;
Assert.That(refreshToken, Is.Not.Null); //On Auth using non IAuthWithRequest
var postAuthRefreshToken = authClient.Send(new Authenticate()).RefreshToken;
Assert.That(postAuthRefreshToken, Is.Null); //After Auth
}
the first code authClient.Send(new Authenticate()).BearerToken can return value ,
why authClient.Send(new Authenticate()).RefreshToken return null, can you tell me the difference?
var postAuthRefreshToken = authClient.Send(new Authenticate()).RefreshToken;
Essentially it prevents someone who has managed to capture an authenticated session to continually call /auth whilst they’re authenticated to continually get new Bearer/Refresh tokens. They need to re-authenticate in order to get new Bearer/Refresh tokens.
so in StatelessAuthTests.cs, the below code is only run once, if run again , will get null.
In servicestack 4.5.6 the below code run again will get newValue, Is it changed in 4.5.8?