“CryptographicException:找不到请求的对象"当证书文件存在时 [英] "CryptographicException: Cannot find the requested object" while the certificate file exists

查看:33
本文介绍了“CryptographicException:找不到请求的对象"当证书文件存在时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .p12 证书文件,我像这样创建我的证书:

I have a .p12 certificate file and I create my certificate like this:

var certificate = new X509Certificate2(certFileLocation, "mySecret", X509KeyStorageFlags.Exportable);

certFileLocation 在我的桌面上并且我给出绝对路径时,代码就可以工作了.但是当我将 .p12 文件的全部内容放在我的解决方案中的一个新文件中并将文件的 Copy to Output Directory 属性设置为 Copy if newer"我收到一个 CryptographicException 异常,内容为:

When certFileLocation is on my desktop and I give the absolute path, the code works. But when I put the entire content of the .p12 file in a new file in my solution and set the Copy to Output Directory property of the file to "Copy if newer" I get a CryptographicException exception that says:

找不到请求的对象

我每次都会检查文件是否到位.这两种情况有什么区别,为什么我不能用后一种方式读取文件?

I also check every time whether the file is in place and it is. What is the difference between these two scenarios and why can't I read the file with the latter approach?

推荐答案

我遇到了类似的问题.它适用于固定文件,但不适用于与我的单元测试相关的文件.这真是令人费解,以至于我终于查看了这些文件并比较了它们的二进制文件.它们相同.它们作为字符串被读取和写入,这导致它们由于 unicode 解释而略有不同.当我自己(从资源流)将它们复制为二进制文件 (byte[]) 时,一切又恢复了.我希望这也能解决您的问题.

I had a similar problem. It worked with a fixed file but did not work with the file relative to my unit tests. This was so mindnumbling that I finally had a look at the files and compared them binary. They where not the same. They were read and written as strings, which caused them to be slightly different due to unicode interpretations. When I copied them myself (from a resourcestream) as binary (byte[]), everything worked again. I hope this solves your problem too.

这篇关于“CryptographicException:找不到请求的对象"当证书文件存在时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆