简体   繁体   English

无法使用X509Certificate2找到指定的对象

[英]Can not find the specified object with X509Certificate2

I'm trying to load a file that contains the secret key to access to Google Calendar API . 我正在尝试加载包含访问Google Calendar API密钥的文件。 Following this tutorial. 遵循本教程。 For doing this I've created this code: 为此,我创建了这段代码:

var certificate = new X509Certificate2("client_secret.json", "notasecret", X509KeyStorageFlags.Exportable);

I've uploaded client_secret.json file inside my solution, this is the path of the file: "...\\Visual Studio 2015\\Projects\\Calendar\\Calendar\\bin\\Debug\\client_secret.json" 我在我的解决方案中上传了client_secret.json文件,这是文件的路径: "...\\Visual Studio 2015\\Projects\\Calendar\\Calendar\\bin\\Debug\\client_secret.json"

but seems that the code can't locate the file and return this error: 但似乎代码无法找到该文件并返回此错误:

Can not find the specified object with X509Certificate2 无法使用X509Certificate2找到指定的对象

I also set the property Always copy on Copy in the output directory on the file to read. 我还在文件Copy in the output directory设置属性Always copy on Copy in the output directory以便阅读。

After a bit 'of headaches I was able to understand where wrong, as I said in the comments I generate the certificate by: 经过一番头痛后,我能够理解哪里出错了,正如我在评论中所说,我通过以下方式生成证书:

The first step that I did is create the API for Google Calendar, later I clicked on "create credentials" and selected "Service account", choosing the API that I've created before and the key type as json. 我做的第一步是为Google日历创建API,稍后点击“创建凭据”并选择“服务帐户”,选择我之前创建的API,键类型为json。

this certificate is different against the key the the X509Certificate2 waiting, so the correct step to do is: 此证书与X509Certificate2等待的密钥不同,因此正确的步骤是:

1 . 1 Click on Manage Service Account on the Developer Console, this workding is on the right of the credentials tab, just a little 'over to your projects list. 单击开发人员控制台上的管理服务帐户,此工作位于凭证选项卡的右侧,只需一点点到您的项目列表。

2 . 2 A new window appears, you need to click on the three dots next to the project you want to create the key. 出现一个新窗口,您需要单击要创建密钥的项目旁边的三个点。 (The three dots are on the right). (三个点在右边)。

3 . 3 A pop-up menu appear, and then, you need to click Create Key . 出现一个弹出菜单,然后,您需要单击“ Create Key

4 . 4 Chose the P12 format, and then click on Create. 选择P12格式,然后单击“创建”。

5 . 5 Save the file downloaded on a folder and link them in your code, in particular: 保存下载到文件夹中的文件并将其链接到您的代码中,特别是:

var certificate = new X509Certificate2(@"C:\key.p12", "notasecret", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet);

note that key.p12 is the name of the file of the certificate, and notasecret is the default password that appear just a bit later of the step 4, in other word is the password associated to the certificate. 请注意, key.p12是证书文件的名称, notasecret是在步骤4稍后出现的默认密码,换句话说就是与证书关联的密码。

So my code seems to find the file and read it correctly without display any errors. 所以我的代码似乎找到了文件并正确读取它而没有显示任何错误。

Thanks anyway to MegaTron that have aroused in me the doubt that the certificate was somehow not right. 非常感谢MegaTron ,这让我怀疑证书不知何故是不对的。

Try to use MachineKeySet . 尝试使用MachineKeySet It means that need to use the local computer store for the key: 这意味着需要使用本地计算机商店作为密钥:

var certificate = new X509Certificate2("client_secret.json", "notasecret", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM