Is there a way to access local User Certificate Store from .Net Blazor web application?

Viewed 983

I cant load user certificate within web assembly (Blazor web app). Here is my code (it's modified example "Get Started with 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;
}

} 

But it fails with exception message "Store MY doesn't exist" or smth. So, I just want to access user certificate store to make digital signatures etc. How can I do it? Thanks.

0 Answers
Related