MAUI Android Connection to SQL Server

PDA is an Android system that calls the Kingdee Cloud Starry Sky web API to implement warehouse material receiving and issuing. However, some functions cannot be achieved by using the read web API, and it is necessary to directly read and write the database, resulting in an error message, as shown in the figure:

Test normal reading using console program.

Google searched around and found a solution that requires the use of SSL certificates. Finally, we found a solution that does not require certificates. Please refer to the address for more information.

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

Operation steps:

Create a new DangerousTrustProvider.cs file under Platforms Android, as shown in the following figure.

Add a namespace.

using System;
using Java.Net;
using Java.Security;
using Java.Security.Cert;
using Javax.Net.Ssl;

Add code.

internal class DangerousTrustProvider : Provider
{
private const string TRUST_PROVIDER_ALG = "DangerousTrustAlgorithm";
private const string TRUST_PROVIDER_ID = "DangerousTrustProvider";
public DangerousTrustProvider() : base(TRUST_PROVIDER_ID, 1, string.Empty)
{
  var key = "TrustManagerFactory." + DangerousTrustManagerFactory.GetAlgorithm();
  var val = Java.Lang.Class.FromType(typeof(DangerousTrustManagerFactory)).Name;
  Put(key, val);
}
public static void Register()
{
  Provider registered = Security.GetProvider(TRUST_PROVIDER_ID);
  if (null == registered)
  {
      Security.InsertProviderAt(new DangerousTrustProvider(), 1);
      Security.SetProperty("ssl.TrustManagerFactory.algorithm", TRUST_PROVIDER_ALG);
  }
}
public class DangerousTrustManager : X509ExtendedTrustManager
{
  public override void CheckClientTrusted(X509Certificate[] chain, string authType, Socket socket) { }
  public override void CheckClientTrusted(X509Certificate[] chain, string authType, SSLEngine engine) { }
  public override void CheckClientTrusted(X509Certificate[] chain, string authType) { }
  public override void CheckServerTrusted(X509Certificate[] chain, string authType, Socket socket) { }
  public override void CheckServerTrusted(X509Certificate[] chain, string authType, SSLEngine engine) { }
  public override void CheckServerTrusted(X509Certificate[] chain, string authType) { }
  public override X509Certificate[] GetAcceptedIssuers() => Array.Empty<X509Certificate>();
}
public class DangerousTrustManagerFactory : TrustManagerFactorySpi
{
  protected override void EngineInit(IManagerFactoryParameters mgrparams) { }
  protected override void EngineInit(KeyStore keystore) { }
  protected override ITrustManager[] EngineGetTrustManagers() => new ITrustManager[] { new DangerousTrustManager() };
  public static string GetAlgorithm() => TRUST_PROVIDER_ALG;
}
}

Copy the code above, paste and replace it, as shown in the figure.

The complete code is shown in the figure.

Open, MauiProgram.cs to add calling code.

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

As shown in the figure.

Test again with the simulator and read successfully.

Android version 7.0; Debugging passed, Android 6.0 cannot be used.