2011-12-21 118 views
2

我通过SSL使用java web服务时遇到问题。 我有两种方法,一种是.net4.0,另一种是.net2.0。 不幸的是.net4.0的方法不起作用。不过,早期版本(2.0)是否正常工作:以.net2.0和.net4.0编写的webservice客户端之间的差异

class Program 
{ 
    static void Main(string[] args) 
    { 
     try 
     { 
      Srv.Service client = new Srv.Service(); 
      X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser); 
      store.Open(OpenFlags.ReadOnly); 
      string findValue = "IssuerName"; 
      X509Certificate2Collection certsCollection = store.Certificates.Find(X509FindType.FindByIssuerName, findValue, false); 

      X509Certificate2 cert; 
      if (certsCollection.Count > 0) 
      { 
       cert = certsCollection[0]; 
       client.ClientCertificates.Add(cert); // Only in .net 2.0 
      } 

      client.MethodA(); 

     } 
     catch (Exception e) 
     { 
      string msg = e.Message; 
     } 
    } 
} 

之后,我做了类似的事情在.net4.0客户端(抛出“无法建立与权威{服务器}为SSL/TLS安全通道”除外)

class Program 
{ 
    static void Main(string[] args) 
    { 
     try 
     { 
      Srv.ServiceClient srv = new Srv.ServiceClient(); 
      X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser); 
      store.Open(OpenFlags.ReadOnly); 
      string findValue = "IssuerName"; 
      X509Certificate2Collection certsCollection = store.Certificates.Find(X509FindType.FindByIssuerName, findValue, false); 

      X509Certificate2 cert; 
      if (certsCollection.Count > 0) 
      { 
       cert = certsCollection[0]; 
       srv.ClientCredentials.ClientCertificate.Certificate = new X509Certificate2(); 
       srv.ClientCredentials.ClientCertificate.Certificate = cert; 
      } 

      client.MethodA(); 
     } 
     catch (Exception e) 
     { 
      string msg = e.Message; 
     } 
    } 
} 

为什么几乎相同的代码工作在2.0和4.0中抛出异常? 或者我在第二个例子中做错了? 重写ServicePointManager.ServerCertificateValidationCallback的没有帮助...

为什么我不能添加用户证书由在4.0加入方法就像是在2.0的框架做了什么?

编辑: 我没有使用IIS。我正在使用JBoss托管的webservice。

在第二个例子中我得到以下异常:

无法建立与权威{服务器}为SSL/TLS安全通道

+0

您使用IIS ..?如果是这样的.NET 4.0有不同的应用程序池,我几乎打赌,2.0的人指向.net 2.0框架..我可能会误.. – MethodMan 2011-12-21 18:40:18

+1

我认为SSL的证书和证书是两个不同的东西。 – 2011-12-21 19:42:01

+0

我认为你是对的。但是,我怎样才能把客户端证书写入客户端4.0? – user1013552 2011-12-21 20:30:14

回答

相关问题