public static HttpWebRequest CreateGetRequest(DigiUser user, string url, byte[] body) { if (user == null || string.IsNullOrEmpty(url) || body == null) return null; if (user.Certificate == null) return null; var request = (HttpWebRequest)WebRequest.Create(new Uri(url)); request.Method = "GET"; request.ContentType = Config.DigipostContentType; request.Accept = Config.DigipostContentType; request.Headers.Add("X-Digipost-UserId", user.Id.ToString()); request.Date = DateTime.Now; request.Headers.Add("Content-MD5", Convert.ToBase64String(MD5.Create().ComputeHash(body))); var certificateSignature = CreateCertificateSignature(request, user); if (string.IsNullOrEmpty(certificateSignature)) return null; request.Headers.Add("X-Digipost-Signature", certificateSignature); return request; }
public static string CreateCertificateSignature(HttpWebRequest request, DigiUser user) { var s = request.Method.ToUpper() + "\n" + request.RequestUri.AbsolutePath.ToLower() + "\n" + "content-md5: " + request.Headers["Content-MD5"] + "\n" + "date: " + request.Date.ToUniversalTime().ToString("r") + "\n" + "x-digipost-userid: " + request.Headers["X-Digipost-UserId"] + "\n" + HttpUtility.UrlEncode(request.RequestUri.Query).ToLower() + "\n"; var rsa = user.Certificate.PrivateKey as RSACryptoServiceProvider; if (rsa == null) return null; var privateKeyBlob = rsa.ExportCspBlob(true); var rsa2 = new RSACryptoServiceProvider(); rsa2.ImportCspBlob(privateKeyBlob); var sha = SHA256.Create(); var hash = sha.ComputeHash(Encoding.UTF8.GetBytes(s)); var signature = rsa2.SignHash(hash, CryptoConfig.MapNameToOID("SHA256")); return Convert.ToBase64String(signature); }
public static HttpWebRequest CreateGetRequest(DigiUser user, string url) { return CreateGetRequest(user, url, new byte[0]); }