Skip to content

Instantly share code, notes, and snippets.

@TechWatching
Created January 28, 2020 13:05
Show Gist options
  • Save TechWatching/5483325936f012388af3bf20ab013ae5 to your computer and use it in GitHub Desktop.
Save TechWatching/5483325936f012388af3bf20ab013ae5 to your computer and use it in GitHub Desktop.
public class UserApiAuthenticationService : IUserApiAuthenticationService
{
private readonly IMemoryCache _memoryCache;
private readonly HttpClient _httpClient;
public UserApiAuthenticationService(HttpClient httpClient, IMemoryCache memoryCache)
{
_httpClient = httpClient;
_memoryCache = memoryCache;
}
public async Task<string> RetrieveToken()
{
DateTime expirationDate;
if (!_memoryCache.TryGetValue("Token", out string token))
{
var body = new { login = "login", password = "password" };
var response = await _httpClient.PostAsync("login", new StringContent(JsonConvert.SerializeObject(body)));
(token, expirationDate) = await response.Content.ReadAsAsync<AuthResponse>();
_memoryCache.Set("Token", token, new DateTimeOffset(expirationDate));
}
return token;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment