如何添加客户端证书,HTTP 请求被客户端身份验证方案禁止'Anonymous'



从自动生成的 Web 服务代理类调用方法时出现以下错误:

System.ServiceModel.Security.MessageSecurityException was unhandle
HResult=-2146233087 Message=HTTP request 被禁止 客户端身份验证方案"匿名"。 Source=mscorlib
StackTrace: 服务器堆栈跟踪: at System.ServiceModel.Channels.HttpChannelUtilities.ValidateAuthentication(HttpWebRequest request, HttpWebResponse response, WebException responseException, HttpChannelFactory'1 factory) at System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest request, HttpWebResponse response, HttpChannelFactory'1 factory, WebException responseException, ChannelBinding channelBinding) at System.ServiceModel.Channels.HttpChannelFactory'1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan 超时) at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout) at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message 消息,时间跨度超时) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, 对象[] outs,TimeSpan 超时) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, 对象[] 输出) at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage 方法调用,代理操作运行时操作) at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage 消息) 在 [0] 处重新引发异常: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData,Int32 类型) 在RPS。LoteNFe.LoteNFeSoap.TesteEnvioLoteRPS(TesteEnvioLoteRPSRequest 请求) 在RPS。LoteNFe.LoteNFeSoapClient.RPS.LoteNFe.LoteNFeSoap.TesteEnvioLoteRPS(TesteEnvioLoteRPSRequest request) in c:\Users\Marcelo\Documents\Visual Studio 2015\Projects\RPS\RPS\Service References\LoteNFe\Reference.cs:line 560 在RPS。LoteNFe.LoteNFeSoapClient.TesteEnvioLoteRPS(Int32 VersaoSchema, String MensagemXML) in c:\Users\Marcelo\Documents\Visual Studio 2015\Projects\RPS\RPS\Service 参考文献\LoteNFe\参考文献.cs:行 567 在RPS。Assinador.button5_Click(Object sender, EventArgs e) in c:\Users\Marcelo\Documents\Visual Studio 2015\项目\RPS\RPS\Assinador.cs:第76行 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message&m, MouseButton button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message&m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message&m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 原因,应用程序上下文上下文) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 原因,应用程序上下文上下文) at System.Windows.Forms.Application.Run(Form mainForm) 在RPS。Program.Main() in c:\Users\Marcelo\Documents\Visual Studio 2015\Projects\RPS\RPS\Program.cs:line 19 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在System.Threading.ThreadHelper.ThreadStart_Context(对象状态) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException: System.Net.WebException HResult=-2146233079 消息 = 远程服务器返回错误:(403) 禁止访问。 源=系统 堆栈跟踪:at System.Net.HttpWebRequest.GetResponse() at System.ServiceModel.Channels.HttpChannelFactory'1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan 超时) 内部异常:

这就是如何称呼它

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

"服务引用"窗口

"服务参考高级设置"窗口

Obs:这些是默认配置。这是一个市政管理网络服务,用于发送所提供的运输或物流服务的电子发票。

我在添加证书所需的某处阅读,例如:

LoteNFeSoapClient loteNFe = new LoteNFeSoapClient();
loteNFe.ClientCertificates.Add(clientCertificate);

但这是不可能的,因为:

  1. 此自动生成的 Web 服务类不继承自类 例如,这种方法ClientCertificates.Add()HttpWebClientProtocol类。
  2. LoteNFeSoapClient()类已经继承自 使消息协定匹配其之间的值的接口 请求和响应。
  3. 在 c# 中,一个类不能有多个基类。

那么,我们能做些什么呢?提前谢谢。

解决了,我刚刚使用 wsdl 通过命令行生成了 webservice 代理类.exe,这个类现在继承自System.Web.Services.Protocols.SoapHttpClientProtocol,所以我可以使用

LoteNFe loteNFe = new LoteNFe();
loteNFe.ClientCertificates.Add(x509Certificate);

注意:通过命令行生成的类与使用 VS2013 UI 生成的类完全不同。

最新更新