【问题标题】:Installing CA certificate on android in system context在系统上下文中在 android 上安装 CA 证书
【发布时间】:2020-06-22 07:06:33
【问题描述】:

我需要通过 HTTPS 从我的 Xamarin APP 访问 REST API。当然,在我的开发环境中,我不使用公开签名的证书,而是从本地 CA 签名。所以我需要将我的 CA 证书添加到 Android 模拟器上的受信任 CA。经过一些谷歌搜索后,我发现的第一种方法是,只需将其拖放到模拟器中,然后使用“文件”应用程序安装它。但这似乎只是将它安装在用户上下文中。我的应用程序似乎并不关心,因为它仍然不接受来自 API 的证书作为受信任的。于是我又找了一些……

下一个方法是从证书中获取颁发者哈希值,在其后重命名文件,禁用 Google APIsGoogle 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 APIsGoogle Play Store 禁用。我不知道如果我禁用这些 API 会有什么不同。

我不敢相信这是安装 CA 证书的唯一方法。它是如何在真实设备上完成的?我想我不能在那里禁用谷歌 API?

【问题讨论】:

  • 我对你的建议是避免弄乱自签名证书,因为这真的很让人头疼。只需使用HTTP 构建您的应用程序,然后我们您终于可以部署、打开 SSL 并使用来自已知权威机构的 CA。 JVM 应该已经在其信任库中附带了该 CA 的证书,您根本不需要进行任何配置工作。
  • 似乎实际上是最简单的方法
  • 我可以确认,自签名证书绝对令人头疼。无法使其在两个平台上都正常工作,并且也切换到HTTP。真是令人失望。

标签: android xamarin.android certificate


【解决方案1】:

Tim Biegeleisen 的评论让我花了一些时间寻找以纯文本形式访问 API 的方向。默认情况下,Android 和 iOS 都不允许这样做。幸运的是,可以将其用于我认为可以接受的解决方案的特定域:

安卓

在这里找到https://devblogs.microsoft.com/xamarin/cleartext-http-android-network-security/

  1. 在Android项目的resources下添加如下文件和文件夹: xml\network_security_config.xml
  2. 在文件中添加这样的行:
    <?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>
  1. Properties\AssemblyInfo.xml 中添加android:networkSecurityConfig 到应用程序头:
    <manifest>
    <application android:networkSecurityConfig="@xml/network_security_config">
        ...
    </application>
    </manifest>

iOS

在这里找到: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 更改为您的域...

【讨论】:

    猜你喜欢
    • 2018-10-21
    • 2020-08-06
    • 2018-03-29
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多