private async Task<AccessToken> AcquireTokenByRequestAsync(Guid tenantId, RestPacket packet, HttpRequestMessage request) { packet.Add("resource", AzureBaseUrl); request.Headers.Add("Accept", "application/json;odata=verbose;charset=utf-8"); request.Content = packet.GetFormData(); var tenantStr = tenantId.ToString(); if (tenantId.Equals(default(Guid))) tenantStr = "common"; request.RequestUri = new Uri(string.Format(AzureTokenUrl, tenantStr)); var httpClient = new HttpClient(); var response = await httpClient.SendAsync(request); if ((int)response.StatusCode < 400) { var content = await response.Content.ReadAsStringAsync(); var token = JsonConvert.DeserializeObject<AccessToken>(content); token.ExpiresOn = new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(token.expires_on); if (token != null) return token; } var errorContent = await response.Content.ReadAsStringAsync(); throw new ApplicationException($"Error {(int)response.StatusCode} - {response.StatusCode.ToString()}: {errorContent}"); }
private async Task<AccessToken> GetTokenAsync(Guid tenantId) { var token = default(AccessToken); var refreshToken = default(AccessToken); if (_tokens.ContainsKey(tenantId)) { if (_tokens[tenantId] != null) { token = _tokens[tenantId].FirstOrDefault(x => x.resource == AzureBaseUrl); refreshToken = _tokens[tenantId].FirstOrDefault(); } else _tokens.Remove(tenantId); } if (refreshToken == null && _tokens.ContainsKey(default(Guid))) refreshToken = _tokens[default(Guid)].FirstOrDefault(); if (token == null || token.ExpiresOn < DateTime.UtcNow.AddMinutes(-1)) token = null; if (token == null && refreshToken != null && refreshToken.refresh_token != null) { try { var request = new HttpRequestMessage(); request.Method = HttpMethod.Post; request.Headers.Add("Accept", "application/json"); request.Headers.Add("x-ms-version", "2016-02-01"); var restPacket = new RestPacket(); restPacket.Add("grant_type", "refresh_token"); restPacket.Add("refresh_token", refreshToken.refresh_token); token = await AcquireTokenByRequestAsync(tenantId, restPacket, request); } catch { token = null; } } if (token == null) { var request = new HttpRequestMessage(); request.Method = HttpMethod.Post; request.Headers.Add("x-ms-version", "2016-02-01"); // Add client secret information var restPacket = new RestPacket(); restPacket.Add("client_id", _connectionData.AzureRMServicePrincipalId); restPacket.Add("redirect_uri", AzureRedirectUrl); restPacket.Add("grant_type", "client_credentials"); restPacket.Add("client_secret", _connectionData.UnsecureDecrypt(_connectionData.AzureRMServicePrincipalKey)); token = await AcquireTokenByRequestAsync(tenantId, restPacket, request); } if (!_tokens.ContainsKey(tenantId)) _tokens.Add(tenantId, new List<AccessToken>()); var existingToken = _tokens[tenantId].FirstOrDefault(x => x.resource == AzureBaseUrl); if (existingToken == null || existingToken.access_token != token.access_token) { if (existingToken != null) _tokens[tenantId].Remove(existingToken); _tokens[tenantId].Add(existingToken); } return token; }