使用证书创建request请求

之前写过的程序,都是普通http request。

这是第一次使用,记录下。

 1 private static X509Certificate2 GetCert(string certId,StoreLocation location)
 2 {
 3        X509Certificate2 result = null;
 4        X509Store certStore;
 5        certStore = new X509Store(StoreName.My, location);
 6        certStore.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);
 7 
 8        try
 9        {
10             X509Certificate2Collection set = certStore.Certificates.Find(
11             X509FindType.FindByThumbprint, certId, true);
12 
13             if (set.Count > 0 && set[0] != null && set[0].HasPrivateKey)
14             {
15                   result = set[0];
16             }
17             }
18          finally
19         {
20              certStore.Close();
21         }
22 
23         return result;
24  }

说明:

命名空间:

using System.Security.Cryptography.X509Certificates;

certID:

证书含有一个指纹(thumbprint),就是certId。在证书的detail中可以查看,注意他们之间的空格,很容易copy错误;

Location:

两个类别:

CurrentUser,LocalMachine。命令行mmc然后找到证书管理。

调用:

req = WebRequest.CreateHttp(url);

// add in the cert we'll authenticate with
req.ClientCertificates.Add(GetCert(thumbprint,StoreLocation.CurrentUser));

 

这样就创建了一个需要证书的请求。

转载于:https://www.cnblogs.com/shy-huang/p/6737517.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值