Created
January 28, 2020 13:05
-
-
Save TechWatching/5483325936f012388af3bf20ab013ae5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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