简体   繁体   English

.net2.0和.net4.0写的webservice客户端的区别

[英]Differences between webservice clients written in .net2.0 and .net4.0

I have a problem with consuming a java webservice over SSL.我在通过 SSL 使用 java webservice 时遇到问题。 I have two approaches, one with .net4.0 and one with .net2.0.我有两种方法,一种使用 .net4.0,另一种使用 .net2.0。 Unfortunately the .net4.0 approach did not work.不幸的是,.net4.0 方法不起作用。 However, the earlier version (2.0) is working correctly:但是,早期版本 (2.0) 可以正常工作:

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;
        }
    }
}

After that I did something similar in .net4.0 client ( throws 'Could not establish secure channel for SSL/TLS with authority {server_name}' Exception) :之后,我在 .net4.0 客户端中做了类似的事情(抛出“无法为 SSL/TLS 建立具有权限 {server_name} 的安全通道”异常)

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;
        }
    }
}

Why is almost the same code is working in 2.0 and throwing an exception in 4.0?为什么几乎相同的代码在 2.0 中工作并在 4.0 中引发异常? Or maybe I am doing it wrong in second example?或者也许我在第二个例子中做错了? Overriding of ServicePointManager.ServerCertificateValidationCallback did not help...覆盖 ServicePointManager.ServerCertificateValidationCallback 没有帮助......

Why I cannot add user certificate by Add method in 4.0 like it is done in 2.0 framework?为什么我不能像在 2.0 框架中那样在 4.0 中通过Add方法添加用户证书?

Edit: I am not using IIS.编辑:我没有使用 IIS。 I am consuming webservice which is hosted on JBoss.我正在使用托管在 JBoss 上的 web 服务。

In second example i get following exception:在第二个示例中,我得到以下异常:

Could not establish secure channel for SSL/TLS with authority {server_name}无法为具有权限 {server_name} 的 SSL/TLS 建立安全通道

I had the same issue.我遇到过同样的问题。 I could invoke web service with soapUI:我可以用soapUI调用网络服务:肥皂界面 My solution - create web reference by using 'Add service reference' dialog (Advanced settings -> Add Web Reference).我的解决方案 - 使用“添加服务引用”对话框(高级设置 -> 添加 Web 引用)创建 Web 引用。 See: Web Reference vs. Service Reference and Difference between web reference and service reference?请参阅: Web 参考与服务参考以及 Web 参考和服务参考之间的区别?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM