public async Task <TokenEntity> GetAccessTokenByClientConfigId(Guid id) { ClientConfiguration clientConfiguration = _ingestionContext.ClientConfigurations.FirstOrDefault(x => x.ClientConfigurationId == id); if (clientConfiguration == null) { return(null); } TokenEntity tokenEntity = await Login(clientConfiguration.LoginUrl, clientConfiguration.AccountLogin, clientConfiguration.AccountPassword); if (tokenEntity != null && tokenEntity.IsSuccess && tokenEntity.ExpireDate > DateTime.Now) { return(tokenEntity); } return(null); }
public async Task <TokenEntity> Login(string url, string email, string password) { try { var requestUrl = url.SetQueryParam("email", email) .SetQueryParam("password", password); string loginResult = await RestClient.GetResponseAsync(requestUrl); if (string.IsNullOrEmpty(loginResult)) { return(null); } TokenEntity tokenEntity = GetTokenEntity(loginResult); return(tokenEntity); } catch (Exception ex) { _logger.LogError(ex.Message); throw new Exception(ex.Message); } }
/// <summary> /// Deserialize the XML result into TokenEntity class /// fixed a bug where the miliseconds expire date sometimes return 6 or 7 digits /// truncate the miliseconds to limit to 6 digits /// </summary> /// <param name="loginResult"></param> /// <returns></returns> private TokenEntity GetTokenEntity(string loginResult) { TokenEntity token = null; XmlDocument doc = new XmlDocument(); try { doc.LoadXml(Regex.Replace(loginResult, @"(xmlns:?[^=]*=[""][^""]*[""])", "", RegexOptions.IgnoreCase | RegexOptions.Multiline)); string tokenExpireDate = string.Format("{0}Z", doc.SelectSingleNode(@"//TokenEntity/expireDate").InnerText.Substring(0, 23)); _logger.LogInformation("GetTokenEntity tokenExpireDate: {0}", tokenExpireDate); token = new TokenEntity { IsSuccess = doc.SelectSingleNode(@"//TokenEntity/IsSuccess").InnerText == "true" ? true : false, Token = doc.SelectSingleNode(@"//TokenEntity/Token").InnerText, ExpireDate = DateTime.ParseExact(tokenExpireDate, "yyyy-MM-ddTHH:mm:ss.fffZ", System.Globalization.CultureInfo.CreateSpecificCulture("en-US")) }; } catch (Exception ex) { _logger.LogError(ex.Message); } return(token); }