public static IEnumerable<string> GetPublicKeysList(IReadOnlyList<Certificate> serverCertificates)
{
for (var i = 0; i < serverCertificates.Count; i++)
{
string publicKey = null;
try
{
var certificate = new X509Certificate2(serverCertificates[0].GetCertificateBlob().ToArray());
var ms = new MemoryStream(certificate.RawData);
using (var reader = new BerReader(ms))
{
var asnCert = reader.ReadToEnd(true);
var pkNode = asnCert.ChildNodes[0].ChildNodes[0].ChildNodes[6]; // тут лежит сам ключ
byte[] pkBuffer = new byte[pkNode.Length + (pkNode.DataOffsetToStream - pkNode.StartPosition)];
ms.Seek(pkNode.StartPosition, SeekOrigin.Begin);
ms.Read(pkBuffer, 0, pkNode.Length + (int)(pkNode.DataOffsetToStream - pkNode.StartPosition)); // чтобы не потерять последние 4 байта
publicKey = Convert.ToBase64String(pkBuffer);
}
}
catch (Exception ex)
{
throw new SecurityException($"Cant get public key {ex.Message}");
}
yield return publicKey;
}
}
HttpClientHandler hch = new HttpClientHandler();
hch.AllowAutoRedirect = false;
HttpClient hc = new HttpClient(hch);
StringContent queryString = new StringContent(string.Format("login={0}&password={1}", Uri.EscapeUriString(username), Uri.EscapeUriString(password));
queryString.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded");
HttpResponseMessage msg = await hc.PostAsync("http://www....", queryString);
string responseBody = await msg.Content.ReadAsStringAsync();
...