public async Task <byte[]?> GetPublicPartOfCertificate(string driveCertificateUrl) { var positionOfFileName = driveCertificateUrl.LastIndexOf("/", StringComparison.Ordinal) + 1; var fileName = driveCertificateUrl.Substring(positionOfFileName); lifetime.Add(() => DeleteFileIfExists(fileName)); var directoryName = Path.GetFileNameWithoutExtension(fileName); lifetime.Add(() => DeleteDirectoryIfExists(directoryName)); if (!File.Exists(fileName)) { var downloaded = await DownloadToFile(driveCertificateUrl, fileName); if (!downloaded) { return(null); } } if (!Directory.Exists(directoryName) || Directory.GetFiles(directoryName).Length == 0) { ZipFile.ExtractToDirectory(fileName, directoryName); } var publicPartOfCert = Path.Combine(directoryName, "cert.cer"); return(await File.ReadAllBytesAsync(publicPartOfCert)); async Task <bool> DownloadToFile(string url, string path) { var responseMessage = await httpClient.GetAsync(new Uri(url, UriKind.Absolute)); if (responseMessage.StatusCode == HttpStatusCode.NotFound) { return(false); } responseMessage.EnsureSuccessStatusCode(); var content = responseMessage.Content; await using var stream = await content.ReadAsStreamAsync().ConfigureAwait(false); await using var fileStream = File.OpenWrite(path); await stream.CopyToAsync(fileStream); return(true); } }
public HttpClientImplementation(string baseUrl, string apiKey, ILifetime lifetime, ILog log) { this.log = log; httpClient = lifetime.Add(new HttpClient { BaseAddress = new Uri(baseUrl, UriKind.Absolute) }); httpClient.DefaultRequestHeaders.Add("X-Kontur-Apikey", apiKey); }
public DriveCertificatesReader(ILifetime lifetime) { this.lifetime = lifetime; httpClient = lifetime.Add(new HttpClient()); }