dis*_*kid 7 c# cryptography x509certificate
我有一个.p12证书文件,我创建了这样的证书:
var certificate = new X509Certificate2(certFileLocation, "mySecret", X509KeyStorageFlags.Exportable);
Run Code Online (Sandbox Code Playgroud)
什么时候certFileLocation在我的桌面上,我给出绝对路径,代码工作.但是当我将.p12文件的全部内容放在我的解决方案中的新文件中Copy to Output Directory并将文件的属性设置为"Copy if newer"时,我得到一个CryptographicException异常,说:
找不到请求的对象
每次我都会检查文件是否到位而且确实存在.这两种情况有什么区别,为什么我不能用后一种方法读取文件?
如果您使用 MS-test,则需要更多位:
runsettings配置为在测试完成后不删除文件夹;这导致我浪费了30分钟的时间!DeploymentItem属性添加到您的TestMethod. 这会将其复制到您的Out文件夹中。使用TestContext.DeploymentDirectory作为您的“根”文件夹:
X509Certificate2 GetCert()
{
var stx = File.Open(Path.Combine(TestContext.DeploymentDirectory, "thecertfile.pfx"), FileMode.Open);
using (BinaryReader br = new BinaryReader(stx))
{
return new X509Certificate2(br.ReadBytes((int)br.BaseStream.Length), "password");
}
}
[TestMethod, DeploymentItem("thecertfile.pfx")]
public void Signing_FlameTest()
{
var cert = GetCert();
Assert.IsNotNull(cert, "GetCert failed");
}
Run Code Online (Sandbox Code Playgroud)