【发布时间】:2017-04-01 23:56:18
【问题描述】:
从自动生成的 Web 服务代理类调用方法时出现以下错误:
System.ServiceModel.Security.MessageSecurityException 未处理
HResult=-2146233087 Message=HTTP 请求被禁止 客户端身份验证方案“匿名”。来源=mscorlib
堆栈跟踪: 服务器堆栈跟踪: 在 System.ServiceModel.Channels.HttpChannelUtilities.ValidateAuthentication(HttpWebRequest 请求,HttpWebResponse 响应,WebException responseException, HttpChannelFactory'1 工厂) 在 System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest 请求,HttpWebResponse 响应,HttpChannelFactory'1 工厂, WebException responseException, ChannelBinding channelBinding) 在 System.ServiceModel.Channels.HttpChannelFactory'1.HttpRequestChannel.HttpChannelRequest.WaitForReply(时间跨度 暂停) 在 System.ServiceModel.Channels.RequestChannel.Request(消息消息,TimeSpan 超时) 在 System.ServiceModel.Dispatcher.RequestChannelBinder.Request(消息 消息,时间跨度超时) 在 System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] 出局,TimeSpan 超时) 在 System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, 对象 [] 出局) 在 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall、ProxyOperationRuntime 操作) 在 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage 信息) 在 [0] 处重新抛出异常: 在 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) 在 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(消息数据& msgData,Int32 类型) 在 RPS.LoteNFe.LoteNFeSoap.TesteEnvioLoteRPS(TesteEnvioLoteRPSRequest 要求) 在 RPS.LoteNFe.LoteNFeSoapClient.RPS.LoteNFe.LoteNFeSoap.TesteEnvioLoteRPS(TesteEnvioLoteRPSRequest 请求)在 c:\Users\Marcelo\Documents\Visual Studio 2015\Projects\RPS\RPS\Service References\LoteNFe\Reference.cs:line 560 在 C:\Users\Marcelo\Documents\Visual 中的 RPS.LoteNFe.LoteNFeSoapClient.TesteEnvioLoteRPS(Int32 VersaoSchema, String MensagemXML) Studio 2015\项目\RPS\RPS\服务 参考文献\LoteNFe\Reference.cs:第 567 行 在 c:\Users\Marcelo\Documents\Visual Studio 中的 RPS.Assinador.button5_Click(Object sender, EventArgs e) 2015\Projects\RPS\RPS\Assinador.cs:第 76 行 在 System.Windows.Forms.Control.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs 事件) 在 System.Windows.Forms.Control.WmMouseUp(消息和 m,MouseButtons 按钮,Int32 点击) 在 System.Windows.Forms.Control.WndProc(消息和 m) 在 System.Windows.Forms.ButtonBase.WndProc(消息和 m) 在 System.Windows.Forms.Button.WndProc(消息和 m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息和 m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(味精和味精) 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 原因, Int32 pvLoopData) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.Run(窗体 mainForm) 在 c:\Users\Marcelo\Documents\Visual Studio 2015\Projects\RPS\RPS\Program.cs:line 19 中的 RPS.Program.Main() 在 System.AppDomain._nExecuteAssembly(RuntimeAssembly 程序集,字符串 [] 参数) 在 System.AppDomain.ExecuteAssembly(字符串 assemblyFile,证据 assemblySecurity,String [] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext、ContextCallback 回调、对象状态、布尔值 保留SyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback 回调, 对象状态, Boolean 保留SyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart() InnerException: System.Net.WebException H结果=-2146233079 Message=远程服务器返回错误:(403) Forbidden。 源=系统 堆栈跟踪: 在 System.Net.HttpWebRequest.GetResponse() 在 System.ServiceModel.Channels.HttpChannelFactory'1.HttpRequestChannel.HttpChannelRequest.WaitForReply(时间跨度 暂停) 内部异常:
这样称呼它
X509Certificate2 x509Certificate = new X509Certificate2();
X509Store store = new X509Store("MY", StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
X509Certificate2Collection collection = ((X509Certificate2Collection)store.Certificates).Find(X509FindType.FindByTimeValid, DateTime.Now, false);
X509Certificate2Collection collection1 = X509Certificate2UI.SelectFromCollection(collection, "Certificados disponiveis", "Selecione o certificado", X509SelectionFlag.SingleSelection);
x509Certificate = collection1[0];
LoteNFeSoapClient loteNFe = new LoteNFeSoapClient();
loteNFe.ClientCredentials.ClientCertificate.SetCertificate(x509Certificate.Subject, StoreLocation.CurrentUser, StoreName.My);
Retorno.Text = loteNFe.TesteEnvioLoteRPS(1, Assinado.Text);
我如何添加此 Web 服务参考:
在 MS Visual Studio 2013 社区中,Project > Add > Service Reference:
Service Reference advanced settings window
Obs:这些是默认配置。这是一个市政网络服务,用于发送所提供的运输或物流服务的电子发票。
我在某处读到了添加证书所必需的内容,例如:
LoteNFeSoapClient loteNFe = new LoteNFeSoapClient();
loteNFe.ClientCertificates.Add(clientCertificate);
但这是不可能的,因为:
- 此自动生成的 Web 服务类不继承自类
有这个方法
ClientCertificates.Add()比如HttpWebClientProtocol类。 -
LoteNFeSoapClient()类已经继承自 使messagecontract匹配值的接口 请求和响应。 - 在 c# 中,一个类不能有多个基类。
那么,有什么办法呢?提前致谢。
【问题讨论】:
标签: c# web-services soap messagecontract