Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

HttpClientService.cs 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System.Net;
  2. namespace Diligent.WebAPI.Business.Services
  3. {
  4. public class HttpClientService : IHttpClientService
  5. {
  6. private const string GoogleApiTokenInfoUrl = "https://www.googleapis.com/oauth2/v3/tokeninfo?id_token={0}";
  7. private string[] SupportedClientsIds = { "" };
  8. private readonly AuthorizationSettings _authSettings;
  9. public HttpClientService(IOptions<AuthorizationSettings> authSettings)
  10. {
  11. _authSettings = authSettings.Value;
  12. }
  13. public async Task<bool> IsTokenValid(string providerToken)
  14. {
  15. var httpClient = new HttpClient();
  16. var requestUri = new Uri(string.Format(GoogleApiTokenInfoUrl, providerToken));
  17. HttpResponseMessage httpResponseMessage;
  18. try
  19. {
  20. httpResponseMessage = httpClient.GetAsync(requestUri).Result;
  21. }
  22. catch
  23. {
  24. return false;
  25. }
  26. if (httpResponseMessage.StatusCode != HttpStatusCode.OK)
  27. {
  28. return false;
  29. }
  30. var response = httpResponseMessage.Content.ReadAsStringAsync().Result;
  31. var googleApiTokenInfo = JsonConvert.DeserializeObject<GoogleApiTokenInfo>(response);
  32. //if (!SupportedClientsIds.Contains(googleApiTokenInfo.aud))
  33. if (googleApiTokenInfo.aud != _authSettings.GoogleClientId)
  34. {
  35. return false;
  36. }
  37. return true;
  38. }
  39. }
  40. }