0

我有一个用 C# MVC 4 编写的应用程序,它将在服务器上运行,该应用程序在项目内部有一个证书,位于 App_Data 文件夹中。我需要获取此证书文件才能访问 Web 服务。服务器是 Windows Server 2012,使用 iis7。

代码在本地按预期运行,但在服务器上不起作用。在那里我收到消息“系统找不到指定的文件”。但是这个文件就在那里(我返回了这个目录和前一个目录的内容,将它们放在浏览器的控制台上)。我试图将证书从 .pfx 重命名为 .pcertx (一个随机的),但它再次在服务器上工作,尽管它在本地工作。

这是我用来尝试获取此文件的代码:

public string getCertificado()
{
    string path = string.Empty;
    try
    {          
        path = HttpContext.Current.Server.MapPath(@"~\App_Data\CERTIFICATE.pcertx");            
        Byte[] rawCert = File.ReadAllBytes(path);
        String certificado = Convert.ToBase64String(rawCert);
        _X509Cert.Import(Convert.FromBase64String(certificado), "PASSWORD", X509KeyStorageFlags.PersistKeySet);
        client.AddCerts(new X509Certificate[] { _X509Cert });
        return string.Empty;    
    }
    catch (Exception ex)
    {
        // In case of error, list the content of the current directory and the previous                
         string[] filePaths = Directory.GetFiles(HttpContext.Current.Server.MapPath(@"~\App_Data"));
         string pastaAnterior = Path.GetFullPath(Path.Combine(HttpContext.Current.Server.MapPath(@"~\App_Data"), @"..\"));
         string[] filePathsAnt = Directory.GetFiles(pastaAnterior);
         return "Cannot get certificate." + ex.Message + "; " + ex.InnerException + ";           Path: " + path + ";            Folder content: " + string.Join("\n",filePaths) + ";         Previous folder content" + string.Join("\n",filePathsAnt);
     }
}

这是我从浏览器控制台得到的:

“Não foi possível obter nenhum certificado。 系统找不到指定的文件。
路径:C:\Unisys\Management\Applications\sicopplustest\App_Data\CERTIFICATE.pcertx;
Caminho Pasta: C:\Unisys\Management\Applications\sicopplustest\App_Data \CERTIFICATE.pcertx C:\Unisys\Management\Applications\sicopplustest\App_Data\DynamicAspx.xsl;

Caminho Pasta anteriorC:\Unisys\Management\Applications\sicopplustest\ApplicationInfo.Debug.xml C:\Unisys\Management\Applications\sicopplustest\ApplicationInfo.Release.xml C:\Unisys\Management\Applications\sicopplustest\ApplicationInfo.xml .. 。”

如果我正确获得了证书,则不会发送任何消息。

我认为这可能与安全性有关,但我该如何解决呢?我不知道如何处理这种情况。

4

0 回答 0