4

如何将上传的文件转换为 X509Certificate2 变量?

我尝试使用以下代码,但它不起作用:

public bool hasPublicKey(FileUpload file)
{
    bool check = false;

    try
    {
         X509Certificate2 cert = (X509Certificate2)file.PostedFile.InputStream;
    }
    catch(Exception)
    {
         check = false;
    }
 }
4

2 回答 2

4

如果上传的文件是有效证书,您应该查看 X509Certificate2 类中的构造函数Import方法。

你会看到你需要这样的东西:

var fileLength = file.PostedFile.ContentLength;
var certdata = new byte[fileLength];

file.FileContent.Read(certData, 0, fileLength);

var cert = new X509Certificate2(certData);

(代码未经验证,但它或非常相似的东西应该可以工作)。

于 2013-05-02T19:53:15.640 回答
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;
    }
于 2013-05-02T20:07:28.480 回答