• MAUI android连接sqlserver


    PDA是android系统,调用金蝶云星空webapi实现仓库收发料,使用读取webapi有些功能无法实现,需要直接读写数据库,读取报错,如图:

    用控制台程序测试正常读取

    google搜索了一圈, 都需要使用ssl证书才能解决,终于找到不用证书的解决办法,参考地址

    c# - "Trust anchor for certification path not found." in a .NET Maui Project trying to contact a local .NET WebApi - Stack Overflow

    操作步骤:

    在Platforms\Android下新建DangerousTrustProvider.cs文件,如下图

    添加命名空间

    1. using System;
    2. using Java.Net;
    3. using Java.Security;
    4. using Java.Security.Cert;
    5. using Javax.Net.Ssl;

    添加代码

    1. internal class DangerousTrustProvider : Provider
    2. {
    3. private const string TRUST_PROVIDER_ALG = "DangerousTrustAlgorithm";
    4. private const string TRUST_PROVIDER_ID = "DangerousTrustProvider";
    5. public DangerousTrustProvider() : base(TRUST_PROVIDER_ID, 1, string.Empty)
    6. {
    7. var key = "TrustManagerFactory." + DangerousTrustManagerFactory.GetAlgorithm();
    8. var val = Java.Lang.Class.FromType(typeof(DangerousTrustManagerFactory)).Name;
    9. Put(key, val);
    10. }
    11. public static void Register()
    12. {
    13. Provider registered = Security.GetProvider(TRUST_PROVIDER_ID);
    14. if (null == registered)
    15. {
    16. Security.InsertProviderAt(new DangerousTrustProvider(), 1);
    17. Security.SetProperty("ssl.TrustManagerFactory.algorithm", TRUST_PROVIDER_ALG);
    18. }
    19. }
    20. public class DangerousTrustManager : X509ExtendedTrustManager
    21. {
    22. public override void CheckClientTrusted(X509Certificate[] chain, string authType, Socket socket) { }
    23. public override void CheckClientTrusted(X509Certificate[] chain, string authType, SSLEngine engine) { }
    24. public override void CheckClientTrusted(X509Certificate[] chain, string authType) { }
    25. public override void CheckServerTrusted(X509Certificate[] chain, string authType, Socket socket) { }
    26. public override void CheckServerTrusted(X509Certificate[] chain, string authType, SSLEngine engine) { }
    27. public override void CheckServerTrusted(X509Certificate[] chain, string authType) { }
    28. public override X509Certificate[] GetAcceptedIssuers() => Array.Empty();
    29. }
    30. public class DangerousTrustManagerFactory : TrustManagerFactorySpi
    31. {
    32. protected override void EngineInit(IManagerFactoryParameters mgrparams) { }
    33. protected override void EngineInit(KeyStore keystore) { }
    34. protected override ITrustManager[] EngineGetTrustManagers() => new ITrustManager[] { new DangerousTrustManager() };
    35. public static string GetAlgorithm() => TRUST_PROVIDER_ALG;
    36. }
    37. }

    复制上面的代码,粘贴替换,如图

    完整代码如图

    打开,MauiProgram.cs增加调用代码

    1. #if ANDROID
    2. Platforms.Android.DangerousTrustProvider.Register();
    3. #endif

    如图

    再次用模拟器测试,读取成功

    android版本7.0+调试通过 ,android6.0无法使用。

  • 相关阅读:
    每日一练 | 华为认证真题练习Day121
    在SSD268G上运行第一个demo
    解决 Failed to get nested archive for entry BOOT-INF/lib/xxx.jar
    docker-compose详解
    亚马逊的推广包括站内推广和站外推广
    5年测试经验之谈:2年功能测试、3年自动化测试,从入门到25k...
    windows安装wsl2以及ubuntu
    hand_mysql
    Unity中Shader的光照模型Lambert
    C语言基础(笔记)+程序设计基础I-选择结构题目详解
  • 原文地址:https://blog.csdn.net/mansai/article/details/132915772