【问题标题】:System.Net.WebException: An SSL error has occurred and a secure connection to the server cannot be madeSystem.Net.WebException:发生 SSL 错误,无法与服务器建立安全连接
【发布时间】:2019-06-26 18:04:57
【问题描述】:

已经找到相同的线程here,但这并没有解决我的问题。

我在 info.plist 中添加了 NSAppTransportSecurity 和 NSAllowsArbitraryLoads。

截图:

添加了this文章中的以下代码。

<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>

我正在使用 HTTP REST API。 运行项目时出现以下异常:

System.Net.WebException:发生 SSL 错误,无法与服务器建立安全连接。 ---> Foundation.NSErrorException: Error Domain=NSURLErrorDomain Code=-1200 “发生 SSL 错误,无法与服务器建立安全连接。” UserInfo={NSLocalizedRecoverySuggestion=您还是要连接到服务器吗?

我是否遗漏了什么或做错了什么?

【问题讨论】:

  • SSL证书有效吗?它是否连接到证书上的主机名?证书是自发的吗?如果您在浏览器中打开 url,您会收到与证书相关的任何提示吗?
  • @Maximilian 我在另一个应用程序中拥有相同的其余 API,该应用程序已经在应用商店中。这是一个公共的rest api,如果它在浏览器中打开没有问题..
  • @Maximilian 我所有的 REST API 都是 HTTP,如果我把它改成 HTTPS 可以吗?
  • 这很可能会解决问题

标签: ios ssl xamarin.forms webexception


【解决方案1】:

因为你必须使用证书。

class ViewController: UIViewController, URLSessionDelegate,URLSessionTaskDelegate {

var urlSession: Foundation.URLSession!

  override func viewDidLoad() {
        super.viewDidLoad()
        urlSession = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: nil)
}

    func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {

        let serverTrust = challenge.protectionSpace.serverTrust
        let certificate = SecTrustGetCertificateAtIndex(serverTrust!, 0)
        let policies = NSMutableArray();
        policies.add(SecPolicyCreateSSL(true, (challenge.protectionSpace.host as CFString)))
        SecTrustSetPolicies(serverTrust!, policies);
        var result: SecTrustResultType = SecTrustResultType(rawValue: 0)!
        SecTrustEvaluate(serverTrust!, &result)
        let isServerTrusted:Bool = (result == SecTrustResultType.unspecified || result == SecTrustResultType.proceed)
        let remoteCertificateData:NSData = SecCertificateCopyData(certificate!)
        let pathToCert = Bundle.main.path(forResource: "certificateName", ofType: "crt")
        let localCertificate:NSData = NSData(contentsOfFile: pathToCert!)!
        let credential:URLCredential = URLCredential(trust: serverTrust!)
        completionHandler(.useCredential, credential)

    }


}

【讨论】:

  • 在哪里需要添加这个类?我尝试向 ios 项目添加一个类,但出现很多错误。是否需要安装任何 NuGet 包?
  • 当您需要使用 URLSession 时,您必须将 URLSessionDelegate,URLSessionTaskDelegate 添加到您的 viewController 并且不要错过您的项目中的证书文件 .crt
  • 抱歉没看懂,能解释一下吗?
  • 我所有的 REST API 都是 HTTP,如果我把它改成 HTTPS 可以吗?
【解决方案2】:

原因:从 iOS 9 开始,iOS 将只允许您的应用程序与默认实现最佳实践安全的服务器通信。必须在 Info.plist 中设置值才能与不安全的服务器通信。您似乎只有AllowInsecureHTTPSLoads,但忘记添加AllowsInsecureHTTPLoads

解决方案:在您的 info.plist 中添加以下代码以信任您的域。

<key>NSAppTransportSecurity</key>
 <dict>
 <key>NSExceptionDomains</key>
 <dict>
  <key>pm-admin.smartwcm.com</key>
  <dict>       
   <key>NSExceptionRequiresForwardSecrecy</key>
   <false/>
   <key>NSExceptionAllowsInsecureHTTPLoads</key>
   <true/>
   <key>NSIncludesSubdomains</key>
   <true/>
   ...... 
  </dict>
 </dict>

这里是a similar issue,您可以参考。

【讨论】:

  • 添加上述代码时出现以下异常:System.Net.WebException:此服务器的证书无效。您可能正在连接到伪装成“pm-admin.smartwcm.com”的服务器,这可能会使您的机密信息面临风险。 ---> Foundation.NSErrorException: Error Domain=NSURLErrorDomain Code=-1202“此服务器的证书无效。您可能正在连接到一个伪装成“pm-admin.smartwcm.com”的服务器,这可能会将您的机密信息面临风险。”
  • 你能分享你的样品吗?
  • 我上次创建了这个示例weak,所以没有在info.plist中添加代码,请您添加它吗?
  • 您的示例中似乎包含您的用户名和密码。建议您删除它以防止侵犯隐私。
  • 我会试试你的演示。
猜你喜欢
  • 2016-02-28
  • 2015-12-21
  • 2020-11-02
  • 2018-06-06
  • 2012-07-25
  • 2013-10-11
  • 2017-09-03
  • 2017-01-22
  • 1970-01-01
相关资源
最近更新 更多