C# 使用安全的Web服务
我对使用web服务是新手,我必须访问一个国家运行的web服务。 我得到了一个app.config文件、一个证书文件.cer和一些一般说明 我以前没有使用过VisualStudio2008Express,但我有15年的vb5/6代码编写经验 无论如何,我使用mmc安装了.cer文件,然后打开了一个新项目并 我添加了System.Runtime.Serialization和System.ServiceModel 我向WSDL链接添加了一个服务引用 我在项目中包括app.config文件 并编写了以下代码C# 使用安全的Web服务,c#,C#,我对使用web服务是新手,我必须访问一个国家运行的web服务。 我得到了一个app.config文件、一个证书文件.cer和一些一般说明 我以前没有使用过VisualStudio2008Express,但我有15年的vb5/6代码编写经验 无论如何,我使用mmc安装了.cer文件,然后打开了一个新项目并 我添加了System.Runtime.Serialization和System.ServiceModel 我向WSDL链接添加了一个服务引用 我在项目中包括app.config文件 并编写了以下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EchoSvcClientConsAppl
{
class Program
{
static void Main(string[] args)
{
ConAp66.myserviceref.ServiceClient clientProxy = new ConAp66.myserviceref.ServiceClient();
clientProxy.ClientCredentials.UserName.UserName = "xx";
clientProxy.ClientCredentials.UserName.Password = "xx";
clientProxy.Open();
if (clientProxy != null)
System.Console.WriteLine("init succeeded ");
else
System.Console.WriteLine("init error");
String ar1 = "xxx";
String ar2 = "xxx";
String ar3 = "xxx";
ConAp66.myserviceref.eRes r = clientProxy.getRes1(ar1, ar2, ar3);
if (r.status != null)
System.Console.WriteLine("ok");
else
System.Console.WriteLine(r.error);
clientProxy.Close();
System.Console.ReadLine();
}
}
}
我在第行中遇到一个错误:
ConAp66.myservicef.eRes r=clientProxy.getRes1ar1,ar2,ar3
说我有令牌身份验证问题
有什么线索吗???
我真的不知道该怎么办,非常感谢你的帮助
更新:
我将跟踪添加到app.config并获得:
安全处理器无法在消息中找到安全标头。
这可能是因为消息是一个不安全的错误,或者是因为
是通信方之间的绑定不匹配。
如果服务配置为安全性,而客户端为
不使用安全性
通过谷歌搜索,我发现这应该是
enableUnsecuredResponse=true
在我的app.config中
我这样做了,但是什么也没发生。我认为您需要接受证书服务器,如下所示
public static bool CertHandler(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors error)
{
// Says that you accept the certificate.
// In production environment you have to really check if you can accept the certificate.
return true;
}
ServicePointManager.ServerCertificateValidationCallback = CertHandler;
在创建代理之前添加这些行
类的命名空间为System.Net
类的命名空间是System.Security.Cryptography。web服务是否在https上?如果是这样,您将需要安装一个证书和可能的代码,以允许进行测试的许可连接。不相关,但If clientProxy!=空块是不必要的-如果它是空的,那么在到达那里之前,您将得到一个NullReferenceException,并且您可以保证它不是空的,因为您正在更新它。请发布您的配置和完整的错误消息。web服务在https上,并且我已经安装了给定的.cer文件。完整错误为InnerException:System.ServiceModel.FaultException Message=FailedAuthentication:无法对安全令牌进行身份验证。我应该发布什么配置?我编辑了标题,因为您的问题几乎肯定与您使用的IDE无关。我运行了项目并获得了通信对象System.ServiceModel.Channels.ServiceChannel无法使用,因为它有故障。错误出现在ConAp66.myserviceref.eRes r=clientProxy.getRes1ar1、ar2、ar3行,就像最初发生的一样