在系统上下文中的 android 上安装 CA 证书
Installing CA certificate on android in system context
我需要通过 HTTPS 从我的 Xamarin 应用程序访问 REST API。当然,在我的开发环境中,我不使用公开签名的证书,而是使用本地 CA 签名的证书。所以我需要将我的 CA 证书添加到 Android 模拟器上的受信任的 CA。经过一些 google 搜索后,我发现的第一种方法是将其拖放到模拟器中,然后使用“文件”应用程序安装它。但这似乎只是将它安装在用户上下文中。我的应用程序似乎并不关心,因为它仍然不接受来自 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 APIs?
Tim Biegeleisen 的评论让我花了一些时间寻找以纯文本形式访问 API 的方向。默认情况下,Android 和 iOS 都不允许这样做。幸运的是,可以允许它用于特定域,我认为这是一个可以接受的解决方案:
Android
在此处找到 https://devblogs.microsoft.com/xamarin/cleartext-http-android-network-security/
- 在Android项目的resources下添加如下文件和文件夹:
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>
iOS
在此处找到:
在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
更改为您的域...
我需要通过 HTTPS 从我的 Xamarin 应用程序访问 REST API。当然,在我的开发环境中,我不使用公开签名的证书,而是使用本地 CA 签名的证书。所以我需要将我的 CA 证书添加到 Android 模拟器上的受信任的 CA。经过一些 google 搜索后,我发现的第一种方法是将其拖放到模拟器中,然后使用“文件”应用程序安装它。但这似乎只是将它安装在用户上下文中。我的应用程序似乎并不关心,因为它仍然不接受来自 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 APIs?
Tim Biegeleisen 的评论让我花了一些时间寻找以纯文本形式访问 API 的方向。默认情况下,Android 和 iOS 都不允许这样做。幸运的是,可以允许它用于特定域,我认为这是一个可以接受的解决方案:
Android
在此处找到 https://devblogs.microsoft.com/xamarin/cleartext-http-android-network-security/
- 在Android项目的resources下添加如下文件和文件夹:
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>
iOS
在此处找到:
在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
更改为您的域...