title: How to add ClientCeritifcates, The HTTP request was forbidden with client authentication scheme 'Anonymous'
body: <p>I'm getting the follwing error when calling a method from a auto-generated web service proxy class:</p>
  
  <blockquote>
    <p>System.ServiceModel.Security.MessageSecurityException was unhandled<br>
    HResult=-2146233087   Message=The HTTP request was forbidden with
    client authentication scheme 'Anonymous'.   Source=mscorlib<br>
    StackTrace:
        Server stack trace: 
           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
    timeout)
           at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
           at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message
    message, TimeSpan timeout)
           at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins,
    Object[] outs, TimeSpan timeout)
           at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins,
    Object[] outs)
           at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage
    methodCall, ProxyOperationRuntime operation)
           at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage
    message)
        Exception rethrown at [0]: 
           at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage
    reqMsg, IMessage retMsg)
           at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&amp;
    msgData, Int32 type)
           at RPS.LoteNFe.LoteNFeSoap.TesteEnvioLoteRPS(TesteEnvioLoteRPSRequest
    request)
           at 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
           at RPS.LoteNFe.LoteNFeSoapClient.TesteEnvioLoteRPS(Int32 VersaoSchema, String MensagemXML) in c:\Users\Marcelo\Documents\Visual
    Studio 2015\Projects\RPS\RPS\Service
    References\LoteNFe\Reference.cs:line 567
           at RPS.Assinador.button5_Click(Object sender, EventArgs e) in c:\Users\Marcelo\Documents\Visual Studio
    2015\Projects\RPS\RPS\Assinador.cs:line 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&amp; m, MouseButtons button, Int32 clicks)
           at System.Windows.Forms.Control.WndProc(Message&amp; m)
           at System.Windows.Forms.ButtonBase.WndProc(Message&amp; m)
           at System.Windows.Forms.Button.WndProc(Message&amp; m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; 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
    reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
    reason, ApplicationContext context)
           at System.Windows.Forms.Application.Run(Form mainForm)
           at 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()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           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
           Message=The remote server returned an error: (403) Forbidden.
           Source=System
           StackTrace:
                at System.Net.HttpWebRequest.GetResponse()
                at System.ServiceModel.Channels.HttpChannelFactory'1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan
    timeout)
           InnerException:</p>
  </blockquote>
  
  <p>This is how a call it</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>How I added this web service reference:</p>
  
  <p>In MS Visual Studio 2013 community, <code>Project &gt; Add &gt; Service Reference</code>:</p>
  
  <p><a href="https://i.stack.imgur.com/SNSAE.png" rel="nofollow noreferrer">Service Reference window</a></p>
  
  <p><a href="https://i.stack.imgur.com/MEC5b.png" rel="nofollow noreferrer">Service Reference advanced settings window</a></p>
  
  <p>Obs: Those are default configs. This a municipal gorvernment webservice used to send eletronic invoices of transportation or logistics services provided.</p>
  
  <p>Ive read somewhere that is necessary to add a certificate like:</p>
  
  <pre><code>LoteNFeSoapClient loteNFe = new LoteNFeSoapClient();
  loteNFe.ClientCertificates.Add(clientCertificate);
  </code></pre>
  
  <p>But its not possible because:</p>
  
  <ol>
  <li>This auto-generated webservice class doesnt inherit from an class
  that has this method <code>ClientCertificates.Add()</code> like <code>HttpWebClientProtocol</code> class for example.</li>
  <li><code>LoteNFeSoapClient()</code> class is already inheriting from a
  interface that makes the messagecontract to match values between
  request and response.</li>
  <li>In c# a class can't have multiple base classes.</li>
  </ol>
  
  <p>So, what could be done? Thanks in advance.</p>