以.net2.0和.net4.0编写的Web服务客户端之间的差异

问题描述 投票:2回答:1

我在使用SSL上的java webservice时遇到了问题。我有两种方法,一种是.net4.0,一种是.net2.0。不幸的是.net4.0方法不起作用。但是,早期版本(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;
        }
    }
}

之后我在.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;
        }
    }
}

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

为什么我不能像4.0在2.0框架中那样通过4.0中的Add方法添加用户证书?

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

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

无法使用权​​限{server_name}为SSL / TLS建立安全通道

c# web-services ssl .net-4.0 .net-2.0
1个回答
0
投票

我遇到过同样的问题。我可以使用soapUI调用Web服务:我的解决方案 - 使用“添加服务引用”对话框(高级设置 - >添加Web引用)创建Web引用。见:Web Reference vs. Service ReferenceDifference between web reference and service reference?

© www.soinside.com 2019 - 2024. All rights reserved.