【问题标题】:how to retrieve certificate information in C# for client authentication如何在 C# 中检索证书信息以进行客户端身份验证
【发布时间】:2015-10-07 16:33:03
【问题描述】:

我正在用 c# 开发一个客户端应用程序,并在本地创建了一个证书。现在我需要在 streamsocketcontrol 对象中传递证书。 例如:

socket = new StreamSocket();
Windows.Security.Cryptography.Certificates.Certificate certificate = await GetClientCert();
StreamSocketControl scontrol = socket.Control;
scontrol.ClientCertificate = certificate;

然后我打电话:

ConnectAsync(hostname, port, tls1.2);

我需要了解 C# 中 GetClientCert() 的实现吗?

【问题讨论】:

  • 看起来GetClientCert 在您的应用程序代码中。我们应该如何知道它是如何实现的?
  • 这是我需要帮助实现的api

标签: c# ssl windows-10 tls1.2


【解决方案1】:

如果您需要从计算机上的证书存储区之一获取证书,您可以执行以下操作:

var clientCertificateStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
clientCertificateStore.Open(OpenFlags.ReadOnly);

var clientCert = clientCertificateStore.Certificates.Cast<X509Certificate2>().FirstOrDefault(c => c.Issuer.Contains("MyIssuerName"));

【讨论】:

  • 谢谢詹姆斯,我不知道为什么,但我无法包含 system.security.cryptography.x509certificates,在密码学下我找不到 x509certificates。我在 Windows 10 上使用 Visual Studio 2015
  • 尝试添加对 System.IdentityModel 的引用
  • 您需要添加对 DLL 的引用。
  • 我尝试通过 nuget 包管理器控制台安装,但收到 Microsoft.IdentityModel 6.1 与 UAP 版本不兼容的错误=10.0
  • 通过打开 Windows Identity Foundation 3.5,我能够找到 Microsoft.IdentityModel.dll。我引用了它,但即使无法看到 x509 证书。
【解决方案2】:

试试这个。此选项与 UWP 兼容

public async Task<Certificate> GetClientCert()
{ 
    try {
        Windows.Storage.StorageFile sampleFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Certificate/clients.pfx"));
        IBuffer certBuffer = await Windows.Storage.FileIO.ReadBufferAsync(sampleFile);
        String encodedCertificate = CryptographicBuffer.EncodeToBase64String(certBuffer);
        Certificate crt = new Certificate(certBuffer);
        return crt;
    }catch(Exception e)
    {
        Debug.WriteLine(e.ToString());
    }
    return null;
}

【讨论】:

    猜你喜欢
    • 2012-01-10
    • 1970-01-01
    • 2014-08-23
    • 2015-10-29
    • 2018-07-05
    • 2019-01-14
    • 2021-02-11
    • 2011-04-09
    • 2013-10-07
    相关资源
    最近更新 更多