我需要通过Xamarin APP通过HTTPS访问REST API。当然,在我的开发环境中,我不使用公开签名的证书,而是使用本地CA签名的证书。因此,我需要将我的CA证书添加到Android模拟器上的受信任CA中。谷歌搜索后,我发现的第一种方法是将其拖放到模拟器中,然后使用“文件”应用程序进行安装。但这似乎只能将其安装在用户上下文中。我的应用似乎并不在乎哪一个,因为它仍然不接受来自API的证书作为受信任证书。所以我搜索了更多...
下一个方法是从证书中获取颁发者哈希值,在文件之后重命名文件,禁用Google APIs
和Google Play Store
并执行以下操作:
emulator -avd <avd_name_here> -writable-system
adb root
adb shell remount
adb push <cert_filename> /system/etc/security/cacerts
adb shell "chmod 664 /system/etc/security/cacerts/<cert_filename>"
adb reboot
这确实有效,但有一些缺点。始终使用-writable-system
启动仿真器,这意味着我必须手动启动而不是从Visual Studio启动仿真器,并保持禁用Google APIs
和Google Play Store
。我不知道如果禁用这些API会有什么不同。
我真的不能相信这是安装CA证书的唯一方法。如何在真实设备上完成?我以为我不能在那里禁用Google API?
答案 0 :(得分:1)
Tim Biegeleisen的评论使我投入了一些时间来寻找以纯文本格式访问API的方向。默认情况下,Android和iOS都不允许这样做。足够幸运的是,有可能允许它用于某些我认为可以接受的解决方案的特定域:
在这里https://devblogs.microsoft.com/xamarin/cleartext-http-android-network-security/
xml\network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">10.0.2.2</domain> <!-- Debug port -->
<domain includeSubdomains="true">xamarin.com</domain>
</domain-config>
</network-security-config>
Properties\AssemblyInfo.xml
中,将android:networkSecurityConfig
添加到应用程序标头中: <manifest>
<application android:networkSecurityConfig="@xml/network_security_config">
...
</application>
</manifest>
在这里找到:https://stackoverflow.com/a/33306373/3883521
在info.plist
中添加如下内容:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>domain.com</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
将domain.com
更改为您的域...