title: MonoTouch Web Service Request over SSL Gets 'authentication or decryption has failed'
body: <p>A web service request over SSL raises a WebException on Monotouch v4.0.4.1:</p>
  
  <pre><code>'Error getting response stream (Write: The authentication or decryption has failed)'
  </code></pre>
  
  <p>Since the server's SSL certificate is self-signed (and btw I think it is not X.509), I am bypassing the certificate validation using ServicePointManager.ServerCertificateValidationCallback. The exact same code works fine on Windows .NET, where the web service call returns the correct result. On Monotouch adding a Writeline shows that the ServerCertificateValidationCallback delegate code is never reached.</p>
  
  <p>Note: Although probably not relevant, the content of the request is SOAP with embedded WS-Security UsernameToken.</p>
  
  <ol>
  <li><p>Has anyone got something like this to work on MonoTouch? Have seen reports of similar symptom but no resolution. The code and stacktrace are below, any comment appreciated. Can email a self-contained test case if wanted.</p></li>
  <li><p>I gather there is an alternative approach using certmgr.exe to store the self-signed server certificate in the local trust store, but can't seem to find that app in the MonoTouch distribution. Could anyone point me to it?   </p></li>
  </ol>
  
  <p>..</p>
  
  <pre><code>public class Application
  {
      static void Main (string[] args)
      {
          UIApplication.Main (args);
      }
  }
  
  // The name AppDelegate is referenced in the MainWindow.xib file.
  public partial class AppDelegate : UIApplicationDelegate
  {
      // This method is invoked when the application has loaded its UI and its ready to run
      public override bool FinishedLaunching (UIApplication app, NSDictionary options)
      {
          // If you have defined a view, add it here:
          // window.AddSubview (navigationController.View);
  
          string soapResponse;
          string soapRequest = @" SOAP envelope is here but omitted for brevity ";
          soapResponse = WebService.Invoke("myOperation", soapRequest);
          window.MakeKeyAndVisible ();
          return true;
      }
  
      // This method is required in iPhoneOS 3.0
      public override void OnActivated (UIApplication application)
      {
      }
  }
  
  
  public class WebService
  {
      public static string Invoke(string operation, string soapRequest)
       // Input parameters: 
      //  operation = WS operation name
      //  soapRequest = SOAP XML request
      // Output parameter:
      //  SOAP XML response
      {
          HttpWebResponse response;
          try
          {
              ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
              ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, ssl) =&gt; true;
              HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://myserver.com:7570/MyEndpoint");
              request.Method = "POST";
              request.Headers.Add("SOAPAction", "/MyEndpoint/" + operation);
              request.ContentType = "text/xml;charset=UTF-8";
              request.UserAgent = "Smartphone";
              request.ContentLength = soapRequest.Length;
              request.GetRequestStream().Write(System.Text.Encoding.UTF8.GetBytes(soapRequest), 0, soapRequest.Length);
              request.GetRequestStream().Close();
              response = (HttpWebResponse)request.GetResponse();
              using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8))
              {
                  return reader.ReadToEnd();
              }
          }
          catch (WebException e)
          {
              throw new WebException(e.Message);
          }
      }
  } 
  </code></pre>
  
  <p>Stack trace (some names changed to protect the innocent, original available on request):</p>
  
  <pre><code>WS.WebService.Invoke (operation="myOperation", soapRequest="&lt;SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" \n\txmlns:ns1=\"http://mycompany/Common/Primitives/v1\" \n\txmlns:ns2=\"http://mycompany/Common/actions/externals/Order/v1\" \n\txmlns:ns3=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\"&gt;\n\t&lt;SOAP-ENV:Header&gt; &lt;wsse:Security SOAP-ENV:mustUnderstand=\"1\" \n\txmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\"&gt; \n\t&lt;wsse:UsernameToken wsu:Id=\"UsernameToken-1\" \n\txmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\"&gt; \n\t&lt;wsse:Username&gt;myusername&lt;/wsse:Username&gt; &lt;wsse:Password \n\tType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\"&gt;mypw&lt;/wsse:Password&gt; \n\t&lt;wsse:Nonce&gt;{0}&lt;/wsse:Nonce&gt; \n\t&lt;wsu:Created xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\"&gt;{1}&lt;/wsu:Created&gt; \n\t&lt;/wsse:UsernameToken&gt; &lt;/wsse:Security&gt; \n\t&lt;/SOAP-ENV:Header&gt;&lt;SOAP-ENV:Body&gt;&lt;ns2:tp_getOrderDetailRequest&gt;&lt;ns2:header&gt;&lt;ns1:source&gt;TEAM&lt;/ns1:source&gt;\n\t&lt;ns1:userAccessKey&gt;12345678901234567&lt;/ns1:userAccessKey&gt;&lt;/ns2:header&gt;\n\t&lt;ns2:OrderId&gt;myid1&lt;/ns2:OrderId&gt;\n\t&lt;ns2:OrderId&gt;myid2&lt;/ns2:OrderId&gt;\n\t&lt;/ns2:tp_getOrderDetailRequest&gt;\n\t&lt;/SOAP-ENV:Body&gt;\n\t&lt;/SOAP-ENV:Envelope&gt;") in /Users/billf/Projects/WS/WS/Main.cs:103
  WS.AppDelegate.FinishedLaunching (app={MonoTouch.UIKit.UIApplication}, options=(null)) in /Users/billf/Projects/WS/WS/Main.cs:52
  MonoTouch.UIKit.UIApplication.Main (args={string[0]}, principalClassName=(null), delegateClassName=(null)) in /Developer/MonoTouch/Source/monotouch/monotouch/UIKit/UIApplication.cs:26
  MonoTouch.UIKit.UIApplication.Main (args={string[0]}) in /Developer/MonoTouch/Source/monotouch/monotouch/UIKit/UIApplication.cs:31
  WS.Application.Main (args={string[0]}) in /Users/billf/Projects/WS/WS/Main.cs:18
  </code></pre>