title: How to consume a local HTTPS ASP.NET Core 2.1 API from Android and iOS simulators
body: <p>I'm working on a Xamarin.Forms app that requires to consume some data exposed in an ASP.NET Core 2.1 API, I'm developing the service and the Xamarin app so everything is running local on MAC OS.</p>
  
  <p>The problem I'm facing is happening when sending a request to the ASP.NET Core API, it seems to be related to the HTTPS protocol enabled by default in ASP.NET Core 2.1, when I run the api it's enabling by default the URL <a href="https://localhost:5001" rel="nofollow noreferrer">https://localhost:5001</a> so that is the URL I'm targeting in the Xamarin App.</p>
  
  <p>In Android I can send the request to the API but once the response is sent back, the xamarin app throws the following exception:</p>
  
  <pre><code>Error in AppLoginViewModel.Login Javax.Net.Ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. ---&gt; Java.Security.Cert.CertificateException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. ---&gt; Java.Security.Cert.CertPathValidatorException: Trust anchor for certification path not found.
     --- End of inner exception stack trace ---
     --- End of inner exception stack trace ---
    at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in &lt;fb2a9245146c43afbafe6628542c6726&gt;:0 
    at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeAbstractVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00014] in &lt;fb2a9245146c43afbafe6628542c6726&gt;:0 
    at Javax.Net.Ssl.HttpsURLConnectionInvoker.Connect () [0x0000a] in &lt;9c89490198b4401e89a0f0bf9e68c7be&gt;:0 
    at Xamarin.Android.Net.AndroidClientHandler+&lt;&gt;c__DisplayClass43_0.&lt;ConnectAsync&gt;b__0 () [0x0005a] in &lt;9c89490198b4401e89a0f0bf9e68c7be&gt;:0 
    at System.Threading.Tasks.Task.InnerInvoke () [0x0000f] in &lt;5b7c58d953774179ac771c086a8fa093&gt;:0 
    at System.Threading.Tasks.Task.Execute () [0x00000] in &lt;5b7c58d953774179ac771c086a8fa093&gt;:0 
  --- End of stack trace from previous location where exception was thrown ---
    at Xamarin.Android.Net.AndroidClientHandler+&lt;DoProcessRequest&gt;d__45.MoveNext () [0x0012e] in &lt;9c89490198b4401e89a0f0bf9e68c7be&gt;:0 
  --- End of stack trace from previous location where exception was thrown ---
    at Xamarin.Android.Net.AndroidClientHandler+&lt;SendAsync&gt;d__40.MoveNext () [0x00230] in &lt;9c89490198b4401e89a0f0bf9e68c7be&gt;:0 
  --- End of stack trace from previous location where exception was thrown ---
    at System.Net.Http.HttpClient+&lt;SendAsyncWorker&gt;d__49.MoveNext () [0x000ca] in &lt;006b95df38b04983b2ec7f1bf1c32608&gt;:0 
  --- End of stack trace from previous location where exception was thrown ---
    at AuditoriasCiudadanas.Mobile.Core.Services.RestService+&lt;Seguridad_ValidarEmailAuditorAsync&gt;d__16.MoveNext () [0x00184] in /Users/jorgeramirez/VSProjects/AuditoriasCiudadanas.Mobile/src/AuditoriasCiudadanas.Mobile.Core/Services/RestService.cs:242 
  --- End of stack trace from previous location where exception was thrown ---
    at AuditoriasCiudadanas.Mobile.Core.Services.AuthService+&lt;ValidarEmailAuditor&gt;d__6.MoveNext () [0x0002b] in /Users/jorgeramirez/VSProjects/AuditoriasCiudadanas.Mobile/src/AuditoriasCiudadanas.Mobile.Core/Services/AuthService.cs:28 
  --- End of stack trace from previous location where exception was thrown ---
    at AuditoriasCiudadanas.Mobile.Core.ViewModels.AppForgotPasswordViewModel+&lt;ValidarEmail&gt;d__27.MoveNext () [0x0006f] in /Users/jorgeramirez/VSProjects/AuditoriasCiudadanas.Mobile/src/AuditoriasCiudadanas.Mobile.Core/ViewModels/AppForgotPasswordViewModel.cs:82 
    --- End of managed Javax.Net.Ssl.SSLHandshakeException stack trace ---
  javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
      at com.android.org.conscrypt.ConscryptFileDescriptorSocket.startHandshake(ConscryptFileDescriptorSocket.java:229)
      at com.android.okhttp.internal.io.RealConnection.connectTls(RealConnection.java:192)
      at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:149)
      at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:112)
      at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:184)
      at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
      at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
      at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
      at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
      at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)
      at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:127)
      at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89)
      at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:26)
  Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
      at com.android.org.conscrypt.TrustManagerImpl.verifyChain(TrustManagerImpl.java:661)
      at com.android.org.conscrypt.TrustManagerImpl.checkTrustedRecursive(TrustManagerImpl.java:539)
      at com.android.org.conscrypt.TrustManagerImpl.checkTrusted(TrustManagerImpl.java:495)
      at com.android.org.conscrypt.TrustManagerImpl.checkTrusted(TrustManagerImpl.java:418)
      at com.android.org.conscrypt.TrustManagerImpl.getTrustedChainForServer(TrustManagerImpl.java:339)
      at android.security.net.config.NetworkSecurityTrustManager.checkServerTrusted(NetworkSecurityTrustManager.java:94)
      at android.security.net.config.RootTrustManager.checkServerTrusted(RootTrustManager.java:88)
      at com.android.org.conscrypt.Platform.checkServerTrusted(Platform.java:208)
      at com.android.org.conscrypt.ConscryptFileDescriptorSocket.verifyCertificateChain(ConscryptFileDescriptorSocket.java:404)
      at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
      at com.android.org.conscrypt.NativeSsl.doHandshake(NativeSsl.java:375)
      at com.android.org.conscrypt.ConscryptFileDescriptorSocket.startHandshake(ConscryptFileDescriptorSocket.java:224)
      ... 12 more
  Caused by: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
      ... 24 more
  </code></pre>
  
  <p>In iOS the behavior is the same, the xamarin app throws an exception when receiving the response</p>
  
  <pre><code>Error in AppLoginViewModel.Login System.Net.WebException: An SSL error has occurred and a secure connection to the server cannot be made. ---&gt; Foundation.NSErrorException: Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo={NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, NSErrorPeerCertificateChainKey=(
      "&lt;cert(0x7fce57040e00) s: localhost i: localhost&gt;"
  ), NSErrorClientCertificateStateKey=0, NSErrorFailingURLKey=https://localhost:5001/api/auth, NSErrorFailingURLStringKey=https://localhost:5001/api/auth NSUnderlyingError=0x600001fccc60 {Error Domain=kCFErrorDomainCFNetwork Code=-1200 "(null)" UserInfo={_kCFStreamPropertySSLClientCertificateState=0, kCFStreamPropertySSLPeerTrust=&lt;SecTrustRef: 0x6000023ce400&gt;, _kCFNetworkCFStreamSSLErrorOriginalValue=-9802, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9802, kCFStreamPropertySSLPeerCertificates=(
      "&lt;cert(0x7fce57040e00) s: localhost i: localhost&gt;"
  )}}, _NSURLErrorRelatedURLSessionTaskErrorKey=(
      "LocalDataTask &lt;31AF1DE2-4C56-4480-9F1D-0E9B7C7B8D9A&gt;.&lt;1&gt;"
  ), _kCFStreamErrorCodeKey=-9802, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask &lt;31AF1DE2-4C56-4480-9F1D-0E9B7C7B8D9A&gt;.&lt;1&gt;, NSURLErrorFailingURLPeerTrustErrorKey=&lt;SecTrustRef: 0x6000023ce400&gt;, NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made.}
     --- End of inner exception stack trace ---
    at System.Net.Http.NSUrlSessionHandler+&lt;SendAsync&gt;d__29.MoveNext () [0x001c3] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/src/Foundation/NSUrlSessionHandler.cs:202 
  --- End of stack trace from previous location where exception was thrown ---
    at System.Net.Http.HttpClient+&lt;SendAsyncWorker&gt;d__48.MoveNext () [0x00080] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClient.cs:276 
  --- End of stack trace from previous location where exception was thrown ---
    at AuditoriasCiudadanas.Mobile.Core.Services.RestService+&lt;Seguridad_ValidarEmailAuditorAsync&gt;d__16.MoveNext () [0x00184] in /Users/me/RestService.cs:242 
  --- End of stack trace from previous location where exception was thrown ---
    at AuditoriasCiudadanas.Mobile.Core.Services.AuthService+&lt;ValidarEmailAuditor&gt;d__6.MoveNext () [0x0002b] in /Users/me/AuthService.cs:28 
  --- End of stack trace from previous location where exception was thrown ---
    at AuditoriasCiudadanas.Mobile.Core.ViewModels.AppForgotPasswordViewModel+&lt;ValidarEmail&gt;d__27.MoveNext () [0x0006f] in /Users/me/AppForgotPasswordViewModel.cs:82 
  </code></pre>
  
  <p>Is not possible to debug locally using HTTPS? What I need to change to be able to debug the mobile app and the api at the same time?</p>
  
  <p>I will appreciate your help</p>