关于X509证书
代码如下:在我的机器上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();
}
}
}