关于X509证书

kkk3k3k 2014-07-14 06:20:31
代码如下:在我的机器上WIn7 32位旗舰版 一起运行正常 可以获取证书信息,在XP的机器上也运行正确

但是在客户的机器上 WIn7 64位旗舰版, StoreName.My和AddressBook都没有得到证书信息,也没有发生异常

现在想知道原因? 是缺少权限还是其他什么原因? 非常感谢 不吝赐教。
------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.IO;

namespace ConsoleApplication1
{
class Program
{

static void Log(string logMessage)
{
using (StreamWriter w = File.AppendText("运行结果.txt"))
{

w.WriteLine(" -- :{0}", logMessage);

}
}

static void ShowMsg(X509Certificate2 cer)
{

Log(string.Format("-----------------------------------------------------------"));
Log(string.Format("主题: {0}", cer.Subject));
Log(string.Format("颁发者: {0}", cer.Issuer));
Log(string.Format("版本: {0}", cer.Version));
Log(string.Format("有效日期: {0}", cer.NotBefore));
Log(string.Format("过期日期: {0}", cer.NotAfter));
Log(string.Format("指纹: {0}", cer.Thumbprint));
Log(string.Format("序列号: {0}", cer.SerialNumber));
Log(string.Format("友好名: {0}", cer.PublicKey.Oid.FriendlyName));
Log(string.Format("公钥格式: {0}", cer.PublicKey.EncodedKeyValue.Format(true)));
Log(string.Format("原始数据长度: {0}", cer.RawData.Length));


}

static void Main(string[] args)
{

Log("#################################################################################");
Log("#################################################################################");

try
{


X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.MaxAllowed);
X509Certificate2Collection storecollection = (X509Certificate2Collection)store.Certificates;
Log("证书个数:"+storecollection.Count.ToString());
foreach (X509Certificate2 x509 in storecollection) ShowMsg(x509);



}
catch(Exception e)
{
Log(e.Message);
}

Log("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
Log("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");


try
{

X509Store store = new X509Store(StoreName.AddressBook, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection storecollection = (X509Certificate2Collection)store.Certificates;
Log("证书个数:" + storecollection.Count.ToString());
foreach (X509Certificate2 x509 in storecollection) ShowMsg(x509);

}
catch(Exception e)
{
Log(e.Message);
}


System.Console.WriteLine("已经运行完成,按任意键退出,结果请看[运行结果.txt]");
System.Console.ReadLine();

}


}
}
...全文
276 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wshcdr 2016-01-17
  • 打赏
  • 举报
回复
关注,字数字数字数字数
宝_爸 2014-07-15
  • 打赏
  • 举报
回复
试试把项目改成x86试一试。
宝_爸 2014-07-15
  • 打赏
  • 举报
回复
好像你用了上面讨论中的方法,也不行。
宝_爸 2014-07-15
  • 打赏
  • 举报
回复
我的个天啊,难道是ms的bug? 以下转自: http://stackoverflow.com/questions/4471715/x509certificatestore-findcertificatebysubjectname-does-not-work-in-x64 It looks like there's a bug in the Find methods of this class which prevents them from working in x64. Specifically they're using IntPtrs to send data to native methods. As a work around you can loop through the certificates in the store and do your comparison manually (see sample below), though even in the hack it would be better to use the thumbprint or serial number. A better solution is to re-plumb your application to use the supported class System.Security.Cryptography.X509Certificates.X509Store to access your certificates. This class does work in 64 bit mode.

foreach(X509Certificate cert in store.Certificates)
{
    if (cert != null && cert.Subject.Contains("CN=my cert bla bla"))
        return cert;
}
Console.WriteLine("Not found!");
kkk3k3k 2014-07-15
  • 打赏
  • 举报
回复
谢谢 我在看看
save4me 2014-07-15
  • 打赏
  • 举报
回复
你的代码没有错误,在WIN8.1 X64添加下编译运行正常。 请确认客户电脑上面安装过证书。

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧