C# 将证书从本地计算机传递到webapi

C# 将证书从本地计算机传递到webapi,c#,client-certificates,C#,Client Certificates,我创建了一个在本地计算机上运行的服务。本地计算机安装了一些用于授权的证书,因此获取访问令牌。 所有这些事情以前都是在本地机器上发生的 GetCertificateFromStore(); AuthenticationResult result = _authContext.AcquireTokenAsync(string ResourceId,ClientAssertionCertificate certificateCredential).Result;

我创建了一个在本地计算机上运行的服务。本地计算机安装了一些用于授权的证书,因此获取访问令牌。 所有这些事情以前都是在本地机器上发生的

        GetCertificateFromStore();
        AuthenticationResult result = _authContext.AcquireTokenAsync(string ResourceId,ClientAssertionCertificate certificateCredential).Result;
        if (result != null)
        {
            AccessToken = result.AccessToken;
            return true;
        }
        return false; 
GetCertificateFromStore()正在设置certificateCredential=new ClientAssertionCertificate(ClientId,certificate)

现在我想把这个证书传递给某个web api,并将访问令牌从那个里返回到本地机器。我能够传递证书,但在web api端,传递的证书显示未找到私钥的错误

// Below is the code I am using to call api for accessing and returning access token

client.PostAsJsonAsync(ApiConfigHelper.getAzureAccessToken, certificateCredential).ContinueWith(task =>
                 {
                     if (task.Status == TaskStatus.RanToCompletion)
                     {
                         var response = task.Result;
                         if (response.IsSuccessStatusCode)
                         {
                            // token = response.Content.ReadAsAsync<string>().Result;
                            token = response.Content.ReadAsStringAsync().Result;
                         }
                     }
                 });
            upload.Wait();
//下面是我用来调用api访问和返回访问令牌的代码
client.PostAsJsonAsync(ApiConfigHelper.getAzureAccessToken,certificateCredential)。继续(任务=>
{
if(task.Status==TaskStatus.RanToCompletion)
{
var response=task.Result;
if(响应。IsSuccessStatusCode)
{
//令牌=response.Content.ReadAsAsync().Result;
token=response.Content.ReadAsStringAsync().Result;
}
}
});
upload.Wait();
X.509证书中不存在私钥。这是我得到的例外

我需要知道我是否可以将证书传递给某个api以获取访问令牌

谢谢
Subham

您不能通过网络将私钥传输到远程计算机。你显然做错了什么。如何生成您的令牌?我已经在上面提到过。我使用AcquireTokenSync方法获取令牌。此方法的第二个参数是coming form----------certificateCredential=新客户端证书CertificateCertificate(ClientId,certificate);其中两个参数为:ClientId=“3483fb8d-9e08-4acf-9f26-xxxxxxxxx”CertificateName=“CN=xxxxxxxxx,OU=TestUsers,OU=Test,OU=XXXXXXXXXXXXXX,DC=XXXXOPS,DC=com”