【问题标题】:Calling a php webservice from .net with a cert使用证书从 .net 调用 php webservice
【发布时间】:2011-03-19 14:13:15
【问题描述】:

我正在使用 c# 中的这段代码来调用由用户/密码和证书保护的 php Web 服务:

string wsurl = ConfigurationManager.AppSettings["Url"];
string wsuser = ConfigurationManager.AppSettings["User"];
string wspass = ConfigurationManager.AppSettings["Pass"];

string url = string.Format(wsurl, param1, param2);

System.Net.CredentialCache oCredentialCache = new System.Net.CredentialCache();
oCredentialCache.Add(new System.Uri(url), "Basic",
    new System.Net.NetworkCredential(wsuser, wspass));

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Timeout = TIMEOUT;
req.Credentials = oCredentialCache;
req.Method = "GET";

AttachCert(req);

//Get the data as an HttpWebResponse object
WebResponse resp = req.GetResponse();

这在我的开发机器上运行时按预期工作,但是当我们将它上传到服务器时,我在调用 GetResponse() 时收到超时错误。

AttachCert() 调用是我将证书附加到请求的地方,如果我评论此行,我不会收到超时,但会出现一个正确的错误,指出由于缺少证书而无法完成调用。 这是 AttachCert 代码:

public static void AttachCert(HttpWebRequest Request)
{
    // Obtain the certificate. 
    string certpath = ConfigurationManager.AppSettings["CertPath"];

    X509Certificate Cert = X509Certificate.CreateFromCertFile(certpath);
    ServicePointManager.CertificatePolicy = new CertPolicy();

    Request.ClientCertificates.Add(Cert);
}

知道为什么这可以在我的机器上工作但不能在服务器上工作吗?我们尝试删除 Web 服务上的证书,并且它按预期工作。所以那个电话显然有些奇怪,但我不知道是什么。

谢谢 维琴察

【问题讨论】:

  • 您能否追踪到 AttachCert 并查看导致超时的内部调用?
  • 我发现开发和生产之间存在防火墙问题。您可以 RDP 到生产服务器并确认您可以使用 IE 浏览到 PHP Web 服务吗?
  • 您为什么同时使用基本身份验证和提供证书?
  • 您使用的是哪个版本的 .net?

标签: c# php web-services certificate


【解决方案1】:

尝试注册此回调并注意任何错误。

public static bool ValidateRemoteCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors policyErrors) {
  // check for errors
  // just return true to accept any certificate (self signed etc)
}

ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(MyClass.ValidateRemoteCertificate);

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多