[英]How to retrieve all certificates in your X509Store
I am using the following code to retrieve all certificates in my PC from an asp.net webapp.我正在使用以下代码从 asp.net webapp 检索我 PC 中的所有证书。 The certificates collection is empty, and I can't understand why.证书集合是空的,我不明白为什么。
I tried impersonating my own user account and I didn't succeed as well.我尝试模拟我自己的用户帐户,但也没有成功。 What am I doing wrong?我究竟做错了什么?
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 :(
Add this line of code to the second line and see how it works:将这行代码添加到第二行,看看它是如何工作的:
store.Open(OpenFlags.ReadOnly);
and then this at the bottom :):然后在底部:):
store.Close();
All in one ...一应俱全...
I have an apache server (xamp) with https.我有一个带 https 的 apache 服务器(xamp)。 I access through https and c# (vs2010) to a PHP upload page我通过 https 和 c# (vs2010) 访问 PHP 上传页面
Install the certificate from ie in the personal folder certificate, for example.例如,从 ie 安装个人文件夹证书中的证书。
To view the certicates run "certmgr.msc" , at least in win7要查看证书运行 "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();
I can find certificates by ...我可以通过...找到证书
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 will have the certificates I care about ... if it is just one then I get first element in the collection. certificateCollection 将拥有我关心的证书......如果它只是一个,那么我将获得集合中的第一个元素。
Look in your certificate store(mmc/add/certificate snap-in/my user account/Certificates - Current User/Personal/Certificates) to see the subject name to make sure "CN=mypc.domainname" is whats actually on the cert.查看您的证书存储(mmc/add/certificate snap-in/my user account/Certificates - Current User/Personal/Certificates)以查看主题名称以确保“CN=mypc.domainname”是证书上的实际内容。
"CN=mypc.domainname"
vs对比
"CN = mypc.domainname"
...etc ...等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.