我正在使用以下代码从asp.net webapp中检索我的电脑中的所有证书。证书集合是空的,我不明白为什么。
我尝试冒充我自己的用户帐户,但我也没有成功。我做错了什么?
var store = new X509Store(StoreLocation.CurrentUser); //StoreLocation.LocalMachine fails too
var certificates = store.Certificates;
foreach (var certificate in certificates)
{
var friendlyName = certificate.FriendlyName;
Console.WriteLine(friendlyName);
}
//original problem: fetch a single certificate by its subject name
X509Certificate2 clientCertificate = CertificateUtility.GetCertificate(StoreName.My, StoreLocation.CurrentUser, "CN=mypc.domainname"); //returns null :(
答案 0 :(得分:53)
将这行代码添加到第二行,看看它是如何工作的:
store.Open(OpenFlags.ReadOnly);
然后在底部:):
store.Close();
答案 1 :(得分:18)
一体化......
我有一个带https的apache服务器(xamp)。我通过https和c#访问(vs2010) 到PHP上传页面
例如,从个人文件夹证书中安装证书。
要查看证书运行“certmgr.msc”,至少在win7中
var store = new X509Store(StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
var certificates = store.Certificates;
foreach (var certificate in certificates)
{
var friendlyName = certificate.FriendlyName;
var xname = certificate.GetName(); //obsolete
Console.WriteLine(friendlyName);
}
store.Close();
string certificateName = "CN=localhost"; //name found in the var xname
X509Store storex = new X509Store(StoreName.My, StoreLocation.CurrentUser);
storex.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certificatesx =
storex.Certificates.Find(X509FindType.FindBySubjectName,
certificateName,
true);
X509Certificate certificatex = certificates[0];
storex.Close();
答案 2 :(得分:3)
我可以通过......找到证书。
var certificateStore = new X509Store(StoreName.TrustedPeople, StoreLocation.LocalMachine);
certificateStore.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
var certificateCollection = certificateStore.Certificates.Find(X509FindType.FindBySubjectName, "mycert.me.com",false);
certificateStore.Close();
var certificate = certificateCollection[0];
certificateCollection将拥有我关心的证书......如果它只是一个,那么我会在集合中获得第一个元素。
答案 3 :(得分:1)
查看证书存储区(mmc / add / certificate管理单元/我的用户帐户/证书 - 当前用户/个人/证书)以查看主题名称以确保“CN = mypc.domainname”实际上是什么证书。
"CN=mypc.domainname"
VS
"CN = mypc.domainname"
...等