我有一个用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ãofoipossível取消nenhum证书。
系统找不到指定的文件。
路径:C:\ Unisys \ Management \ Applications \ sicopplustest \ App_Data \ CERTIFICATE.pcertx;
卡米尼奥意大利面:
C:\ Unisys \ Management \ Applications \ sicopplustest \ App_Data \ CERTIFICATE.pcertx
C:\ Unisys \ Management \ Applications \ sicopplustest \ App_Data \ DynamicAspx.xsl;
Caminho意大利面前路C:\ Unisys \ Management \ Applications \ sicopplustest \ ApplicationInfo.Debug.xml C:\ Unisys \ Management \ Applications \ sicopplustest \ ApplicationInfo.Release.xml C:\ Unisys \ Management \ Applications \ sicopplustest \ ApplicationInfo.xml ...”
如果我正确获得了证书,则不会发送任何消息。
我认为这可能与安全性有关,但是如何解决?我不知道该如何处理这种情况。