【问题标题】:How can I load client certificates from personal store using ASP.NET?如何使用 ASP.NET 从个人存储中加载客户端证书?
【发布时间】:2011-11-09 09:43:06
【问题描述】:

如何使用 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";
                }
            }
        }
    }
}

【问题讨论】:

  • 您是在尝试加载访问者证书还是服务器上用户帐户的证书?
  • 我尝试加载访问者证书,注意:我的所有访问者都在 Intranet 网络中并且都登录到活动目录
  • 好的。你如何验证你的用户?他们被冒充了吗?
  • 你真是个天才,我刚刚将默认配置 webconfig 更改为 就可以了,谢谢
  • 很高兴能帮上忙!如果您想将其标记为已接受,我已对我的评论进行了答复。

标签: c# asp.net cryptography x509certificate rsacryptoserviceprovider


【解决方案1】:

所有 Google 员工:

将此添加到您的Web.Config

<identity impersonate="true" /> 

有关详细信息,请参阅msdn on impersonation

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 1/你必须知道我需要在客户端存储“个人”上加载证书而不是在网络服务器存储上 2/是的,它不是生产版本,我只是测试这个代码来实现在实际应用程序 3/asp.net 加载客户端证书时必须破坏的帐户是什么??
    • 多年来我没有对证书存储做过任何事情,但我相信并且似乎有道理,要在个人存储中访问证书,您需要在导入的帐户下运行证书进入所述商店。我建议您在控制台应用程序中运行您的测试工具。在您用于导入证书的同一帐户下。进入商店。
    猜你喜欢
    • 1970-01-01
    • 2019-11-08
    • 2010-12-11
    • 2017-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    相关资源
    最近更新 更多