fix(tenpayv2): 修复 Windows 环境下潜在的证书加载失败问题

This commit is contained in:
Fu Diwei
2023-05-25 18:50:16 +08:00
parent c036640388
commit 12b4294387
3 changed files with 27 additions and 7 deletions

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net461; netstandard2.0; net6.0</TargetFrameworks>
<TargetFrameworks>net461; net471; netstandard2.0; net6.0</TargetFrameworks>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
<NullableReferenceTypes>true</NullableReferenceTypes>
@@ -36,8 +36,8 @@
</ItemGroup>
<ItemGroup>
<Reference Include="System.Net.Http.WebRequest" Condition="'$(TargetFramework)' == 'net461' Or '$(TargetFramework)' == 'net47'" />
<Reference Include="System.Web" Condition="'$(TargetFramework)' == 'net461' Or '$(TargetFramework)' == 'net47'" />
<Reference Include="System.Net.Http.WebRequest" Condition="'$(TargetFramework)' == 'net461' Or '$(TargetFramework)' == 'net471'" />
<Reference Include="System.Web" Condition="'$(TargetFramework)' == 'net461' Or '$(TargetFramework)' == 'net471'" />
</ItemGroup>
<ItemGroup>

View File

@@ -1,5 +1,7 @@
using System.Net.Http;
using System;
using System.Net.Http;
using System.Net.Security;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV2.Settings
@@ -27,7 +29,25 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV2.Settings
if (_certBytes != null)
{
X509Certificate x509 = (_certPassword == null) ? new X509Certificate2(_certBytes) : new X509Certificate2(_certBytes, _certPassword);
X509Certificate x509;
#if NET471_OR_GREATER || NET5_0_OR_GREATER || NETSTANDARD2_0_OR_GREATER
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
#else
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
#endif
{
x509 = (_certPassword == null)
? new X509Certificate2(_certBytes)
: new X509Certificate2(_certBytes, _certPassword, X509KeyStorageFlags.MachineKeySet);
}
else
{
x509 = (_certPassword == null)
? new X509Certificate2(_certBytes)
: new X509Certificate2(_certBytes, _certPassword);
}
handler.ClientCertificates.Add(x509);
}

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net461; net47; netstandard2.0; net6.0</TargetFrameworks>
<TargetFrameworks>net461; net471; netstandard2.0; net6.0</TargetFrameworks>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
<NullableReferenceTypes>true</NullableReferenceTypes>
@@ -36,7 +36,7 @@
</ItemGroup>
<ItemGroup>
<Reference Include="System.Web" Condition="'$(TargetFramework)' == 'net461' Or '$(TargetFramework)' == 'net47'" />
<Reference Include="System.Web" Condition="'$(TargetFramework)' == 'net461' Or '$(TargetFramework)' == 'net471'" />
</ItemGroup>
<ItemGroup>