Android - 无法使用任何临时 SqlClient 版本(v2.1.4、v4.1.0、v5Preview)连接到 SQL Server Express 2019
A connection was successfully established with the server, but then an error occurred during the pre-login handshake.
设法用证书和 IP 地址解决它。
- 使用 powershell 为您的 IP 地址创建证书:
New-SelfSignedCertificate -certstorelocation cert:\localmachine\my -dnsname '192.168.0.15','localhost' -KeySpec KeyExchange -FriendlyName '192.168.0.15' -NotAfter (Get-Date).AddMonths(240)
(稍后将与您的 sql server 的任何 IP 地址一起使用)。
-
在 SQL Server 配置管理器中设置您的证书。
-
将该证书导出为 Base-64 编码的 X.509 (.CER) 文件。
-
在
Platforms\Android\Resources\raw
文件夹中导入该 cer(如果它不存在,请创建它)。 -
在
Platforms\Android\xml
文件夹中创建nsc.xml
(如果我将其命名为 network_security_config.xml 则无法正常工作),
内容为:(更改数据 ofc IP 和域名,@raw/certname 我的名称为 razvoj1.cer 在Platforms\Android\Resources\raw
文件夹和我的 SQL 服务器 IP 是 192.168.0.15 )
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
<certificates src="user" />
<certificates src="@raw/razvoj1"/>
</trust-anchors>
</base-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">localhost</domain>
<domain includeSubdomains="true">192.168.0.15,1433</domain>
<domain includeSubdomains="true">192.168.0.15</domain>
<domain includeSubdomains="true">RAZVOJ1</domain>
<domain includeSubdomains="true">RAZVOJ1.localhost</domain>
<trust-anchors>
<certificates src="system" />
<certificates src="user" />
<certificates src="@raw/razvoj1"/>
</trust-anchors>
</domain-config>
</network-security-config>
- 在 AndroidManifest 中添加
android:usesCleartextTraffic="true"
和android:networkSecurityConfig = "@xml/nsc"
到应用程序标签:
<?xml version="1.0" encoding="utf-8"?>
<manifest android:targetSandboxVersion="1" xmlns:android="http://schemas.android.com/apk/res/android">
<application android:allowBackup="true"
android:icon="@mipmap/appicon"
android:roundIcon="@mipmap/appicon_round"
android:usesCleartextTraffic="true"
android:networkSecurityConfig="@xml/nsc"
android:supportsRtl="true"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
- 使用此连接字符串:
string connectionString = @"Server=192.168.0.15,1433;Database=YOURDB;User Id=sa;Password=YOURPASS;Persist Security Info=True;Encrypt=True;TrustServerCertificate=True";
参考链接
https://github.com/dotnet/SqlClient/issues/1662#issuecomment-1260858334
关联项目
FreeSql QQ群:4336577
BA & Blazor QQ群:795206915
Maui Blazor 中文社区 QQ群:645660665
知识共享许可协议
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名AlexChow(包含链接: https://github.com/densen2014 ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系 。
转载声明
本文来自博客园,作者:周创琳 AlexChow,转载请注明原文链接:https://www.cnblogs.com/densen2014/p/17490263.html
AlexChow
今日头条 | 博客园 | 知乎 | Gitee | GitHub
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 易语言 —— 开山篇