在WebApi中使用FindPrivateKey

时间:2015-12-10 18:58:20

标签: asp.net-web-api certificate

我下载了FindPrivateKey.exe的源代码。

我使用此exe来查找私钥的文件名和文件夹。我必须在机器上本地运行它。我正在寻找一种使用web api远程获取此信息的方法。

FindPrivateKey.exe本身就是一个控制台应用程序。

但在我的webapi代码中,它不起作用(CryptAcquireCertificatePrivateKey返回false)

这有明显的原因吗? web api站点启用了AllowAnonymousAccess,这是为什么?

我遇到包含代码段的问题。总结: StoreName是“我的” StoreLocation是“本地机器”

//the call below returns false and this is where I am stuck
if (CryptAcquireCertificatePrivateKey(cert.Handle,
acquireFlags, IntPtr.Zero, ref hProvider, ref _keyNumber, ref freeProvider))
{
...
}

1 个答案:

答案 0 :(得分:0)

我能够通过首先冒充拥有私钥信息权限的用户来实现这一点。对于任何感兴趣的人,请点击下面的DllImport代码了解如何执行此操作。

    [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
    public static extern int LogonUser(
        string lpszUserName,
        String lpszDomain,
        String lpszPassword,
        int dwLogonType,
        int dwLogonProvider,
        ref IntPtr phToken);

    [DllImport("advapi32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
    public extern static int DuplicateToken(
        IntPtr hToken,
        int impersonationLevel,
        ref IntPtr hNewToken);