3

如何使用 ASP.NET 从个人存储中加载客户端证书?

如果可能的话,我可以用它加密数据吗?

为此,我在 ASP.NET 2.0 中创建了一个应用程序,该应用程序检索安装在客户端证书存储(个人)中的所有证书,以使用它创建数字签名。

但它不起作用,我不知道是什么问题

// ...
using System.Security.Cryptography.X509Certificates;

namespace WebApplication4
{
    public partial class _Default : System.Web.UI.Page
    {
        public static string ToHexString(byte[] bytes)
        {
           // ...
        }

        protected void btnSignature_Click(object sender, EventArgs e)
        {
            X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            store.Open(OpenFlags.OpenExistingOnly);
            X509Certificate2Collection certificates = store.Certificates;

            int a = certificates.Count;

            lbCertCount.Text = System.Convert.ToString(a);

            if (a > 0)
            {
                X509Certificate2 certificate = certificates[1];

                string publicKey = certificate.GetPublicKeyString();
                lbMypublicKey.Text = publicKey + "<br/>";

                // AsymmetricAlgorithm privateKey = certificate.PrivateKey;

                RSACryptoServiceProvider privateKey = certificate.PrivateKey as RSACryptoServiceProvider;

                // test message 
                byte[] buffer = Encoding.Default.GetBytes("Welcome");
                byte[] signature = privateKey.SignData(buffer, new SHA1Managed());
                string me = ToHexString(signature);

                lbSignature.Text = me;


                RSACryptoServiceProvider publicKey1 = certificate.PublicKey.Key as RSACryptoServiceProvider;

                bool verify = publicKey1.VerifyData(buffer, new SHA1Managed(), signature);

                if (verify == true)
                {
                    lbControl.Text = "Signature valid";
                }
                else
                {
                    lbControl.Text = "Signature not Valid";
                }
            }
        }
    }
}
4

2 回答 2

1

对于所有 Google 员工:

将此添加到您的Web.Config

<identity impersonate="true" /> 

有关详细信息,请参阅有关模拟的 msdn

于 2011-11-10T13:03:45.187 回答
0

我猜你已经搞定了这个 ASP.NET 应用程序。作为测试工具,您实际上并不打算编写访问个人证书的生产应用程序。存储在网络服务器上?我根本没有研究或测试过您的代码,但我会首先检查安全特权。我希望运行 ASP.Net 工作进程的帐户无权访问个人证书。店铺。

于 2011-11-09T09:47:54.753 回答