System.Net.WebException: An SSL error has occurred and a secure connection to the server...

SreeeeSreeee INMember ✭✭✭✭✭

Already found the same thread here, but that not resolved my problem.

I have added NSAppTransportSecurity and NSAllowsArbitraryLoads in info.plist.

Screenshot:

enter image description here

Added the below codes from this article.

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
        <key>NSExceptionDomains</key>
        <dict>
            <key>pm-admin.smartwcm.com</key>
            <dict>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSExceptionAllowInsecureHTTPSLoads</key>
                <true/>
                <key>NSExceptionRequiresForwardSecrecy</key>
                <true/>
                <key>NSExceptionMinimumTLSVersion</key>
                <string>TLSv1.1</string>
                <key>NSThirdPartyExceptionAllowInsecureHTTPSLoads</key>
                <false/>
                <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
                <true/>
                <key>NSThirdPartyExceptionMinimumTLSVersion</key>
                <string>TLSv1.1</string>
                <key>NSRequiresCertificateTransparency</key>
                <false/>
            </dict>
        </dict>
    </dict>

I am using HTTP REST APIs. When running the project I am getting the following exception:

System.Net.WebException: An SSL error has occurred and a secure connection to the server cannot be made. ---> 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?

Am I missing something or do anything wrong?

Best Answer

  • LandLuLandLu Xamurai
    Accepted Answer

    Why do you add both NSAllowsArbitraryLoads and NSExceptionDomains in your key NSAppTransportSecurity. If you want your whole app to skip secure connection, you could set it like:

    <key>NSAppTransportSecurity</key>  
     <dict>  
          <key>NSAllowsArbitraryLoads</key>
          <true/>  
     </dict>
    

    If you only want a specific domain to use insecure connection(http), you should set:

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>yourdomain.com</key>
            <dict>
                <!--Include to allow subdomains-->
                <key>NSIncludesSubdomains</key>
                <true/>
                <!--Include to allow HTTP requests-->
                <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <!--Include to specify minimum TLS version-->
                <key>NSTemporaryExceptionMinimumTLSVersion</key>
                <string>TLSv1.1</string>
            </dict>
        </dict>
    </dict>
    

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai
    Accepted Answer

    Why do you add both NSAllowsArbitraryLoads and NSExceptionDomains in your key NSAppTransportSecurity. If you want your whole app to skip secure connection, you could set it like:

    <key>NSAppTransportSecurity</key>  
     <dict>  
          <key>NSAllowsArbitraryLoads</key>
          <true/>  
     </dict>
    

    If you only want a specific domain to use insecure connection(http), you should set:

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>yourdomain.com</key>
            <dict>
                <!--Include to allow subdomains-->
                <key>NSIncludesSubdomains</key>
                <true/>
                <!--Include to allow HTTP requests-->
                <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <!--Include to specify minimum TLS version-->
                <key>NSTemporaryExceptionMinimumTLSVersion</key>
                <string>TLSv1.1</string>
            </dict>
        </dict>
    </dict>
    
  • SreeeeSreeee INMember ✭✭✭✭✭

    Hi @LandLu I added like below:

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>pm-admin.smartwcm.com</key>
            <dict>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSTemporaryExceptionMinimumTLSVersion</key>
                <string>TLSv1.1</string>
            </dict>
        </dict>
    </dict>
    

    But still getting the following exception:

    System.Net.WebException: The certificate for this server is invalid. You might be connecting to a server that is pretending to be “pm-admin.smartwcm.com” which could put your confidential information at risk. ---> Foundation.NSErrorException: Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “pm-admin.smartwcm.com” which could put your confidential information at risk." UserInfo={NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?

  • LandLuLandLu Member, Xamarin Team Xamurai

    It seems this is a server-side issue from your exception above. Please check whether your server has used a valid certification.
    All we can do on our app side is disabling the ATS.

  • abvabv USMember ✭✭✭

    Hi @Sreeee I am also facing same exception

    System.Net.WebException: The certificate for this server is invalid. You might be connecting to a server that is pretending to be “pm-admin.smartwcm.com” which could put your confidential information at risk. ---> Foundation.NSErrorException: Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “pm-admin.smartwcm.com” which could put your confidential information at risk." UserInfo={NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway

    any help for this??
    Thank you.

  • SreeeeSreeee INMember ✭✭✭✭✭
    edited May 22

    @AsV Did you try the accepted answer by @LandLu ?
    Similar issue, please go through this also.

  • abvabv USMember ✭✭✭

    Thank you @Sreeee it is working now.

  • SreeeeSreeee INMember ✭✭✭✭✭

    @AsV said:
    Thank you @Sreeee it is working now.

    Happy coding :)

  • takthetanktakthetank Member ✭✭

    Hello all, I'm facing the same issue when running the project from VS2019, and the accepted answer does not work!
    When running from VS2017, everything works fine.
    Any suggestions?

  • SreeeeSreeee INMember ✭✭✭✭✭

    @takthetank Faced the same issue in many projects. I solved it with the accepted answer solution. :)

  • takthetanktakthetank Member ✭✭

    @Sreeee Unfortunately, the accepted answer solution doesn't work with latest VS2019 update (an other member has the same issue here).

Sign In or Register to comment.