如何将上传的文件转换为X509Certificate2变量?
我尝试使用以下代码,但它无效:
public bool hasPublicKey(FileUpload file)
{
bool check = false;
try
{
X509Certificate2 cert = (X509Certificate2)file.PostedFile.InputStream;
}
catch(Exception)
{
check = false;
}
}
答案 0 :(得分:4)
如果上传的文件是有效证书,您应该查看X509Certificate2类中的constructor或Import方法。
你会发现你需要这样的东西:
var fileLength = file.PostedFile.ContentLength;
var certdata = new byte[fileLength];
file.FileContent.Read(certData, 0, fileLength);
var cert = new X509Certificate2(certData);
(代码未经验证,但它或类似的东西应该有效)。
答案 1 :(得分:1)
public bool hasPublicKey(FileUpload file)
{
bool check = false;
try
{
X509Certificate2 cert = new X509Certificate2(file.FileBytes);
if (cert.PublicKey != null)
{
check = true;
}
}
catch(Exception)
{
check = false;
}
return check;
}