我无法在Web程序集(Blazor Web应用程序)中加载用户证书。 这是我的代码(经过修改的示例“ Blazor入门”)。
...added strings to _ViewImports.cshtml:
@using System.Security.Cryptography;
@using System.Security.Cryptography.X509Certificates;
@using System.Security;
...
@page "/counter"
<h1>Counter</h1>
<p>Current count: @currentCount</p>
<p>Cert Name: @cname</p>
<button class="btn btn-primary" onclick="@IncrementCount">Click me</button>
@functions {
int currentCount = 0;
string cname = "";
public X509Certificate2 UserCert;
void IncrementCount()
{
currentCount++;
UserCert = selectCert();
cname = UserCert?.GetNameInfo(X509NameType.SimpleName, false);
}
public X509Certificate2 selectCert()
{
X509Certificate2 certSelected = null;
try
{
X509Store x509Store = new X509Store("MY", StoreLocation.CurrentUser);
x509Store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
X509Certificate2Collection col = x509Store.Certificates;
X509Certificate2Collection fcollection = (X509Certificate2Collection)col.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
if (fcollection.Count > 0)
{
certSelected = fcollection[0];
}
x509Store.Close();
}
catch (Exception ex)
{
cname = ex.Message;
}
return certSelected;
}
}
但是它失败,并显示异常消息“ Store MY不存在”或smth。因此,我只想访问用户证书存储区以进行数字签名等。我该怎么办?谢谢。