首页 > 解决方案 > 将 IOS 应用程序连接到 asp.net API 时获得 ATS 失败的系统信任。错误代码 9813

问题描述

我有一个 IOS 应用程序和一个托管在同一网络上的 dot net API。我有一个 IP wxyz 我试图通过 info.plist 允许,因为它没有 SSL 证书。我知道不支持 IP 地址,所以我使用 xip.io 将其转换为域名。

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoadsInWebContent</key>
        <true/>
        <key>NSExceptionDomains</key>
        <dict>
            <key>w.x.y.z.xip.io</key>
            <dict>
                <key>NSAllowsLocalNetworking</key>
                <true/>
                <key>NSAllowsArbitraryLoads</key>
                <true/>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSExceptionMinimumTLSVersion</key>
                <string>TLSv1.2</string>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSExceptionRequiresForwardSecrecy</key>
                <false/>
            </dict>
        </dict>
    </dict>

我的斯威夫特代码:

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let url = URL(string: "https://w.x.y.z.xip.io:44328/api/values/1")!

        print("START")
        let task = URLSession.shared.dataTask(with: url) {(data, response, error) in
            guard let data = data else { return }
            print(String(data: data, encoding: .utf8)!)
        }
        print("DONE")

        task.resume()

    }

转到https://wxyzxip.io:44328/api/values/1在我的浏览器中完美运行。当我尝试运行它时,我得到

019-01-25 01:14:10.427331-0500 test_api[20555:3495649] ATS failed system trust
2019-01-25 01:14:10.427505-0500 test_api[20555:3495649] System Trust failed for [1:0x600000f8c900]
2019-01-25 01:14:10.427584-0500 test_api[20555:3495649] TIC SSL Trust Error [1:0x600000f8c900]: 3:0
2019-01-25 01:14:10.432453-0500 test_api[20555:3495649] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
2019-01-25 01:14:10.432601-0500 test_api[20555:3495649] Task <964CC4A4-C3C3-4349-80AA-E1DFC04920DA>.<1> HTTP load failed (error code: -1200 [3:-9802])
2019-01-25 01:14:10.433065-0500 test_api[20555:3495648] Task <964CC4A4-C3C3-4349-80AA-E1DFC04920DA>.<1> finished with error - code: -1200

标签: iosswiftapisslcertificate

解决方案


推荐阅读