Skip to content
This repository has been archived by the owner on Sep 16, 2020. It is now read-only.

Commit

Permalink
Merge pull request #13 from dotnetcore/dev
Browse files Browse the repository at this point in the history
update version to 2.2.7
  • Loading branch information
stulzq authored May 17, 2018
2 parents 39d33f9 + d28385a commit fd32b69
Show file tree
Hide file tree
Showing 18 changed files with 233 additions and 207 deletions.
11 changes: 7 additions & 4 deletions Alipay.AopSdk.AspnetCore/Alipay.AopSdk.AspnetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>2.2.6</Version>
<AssemblyVersion>2.2.6.0</AssemblyVersion>
<FileVersion>2.2.6.0</FileVersion>
<Version>2.2.7</Version>
<AssemblyVersion>2.2.7.0</AssemblyVersion>
<FileVersion>2.2.7.0</FileVersion>
<Copyright>Copyright 2017-2018 stulzq</Copyright>
<Authors>stulzq</Authors>
<Company>stulzq</Company>
Expand All @@ -14,12 +14,15 @@
<PackageProjectUrl>https://github.com/stulzq/Alipay.AopSdk.Core</PackageProjectUrl>
<PackageIconUrl>https://www.alipay.com/favicon.ico</PackageIconUrl>
<Description>支付宝(Alipay)服务端SDK AopSdk,ASP.NET Core支持。</Description>
<PackageReleaseNotes></PackageReleaseNotes>
<PackageReleaseNotes>添加快捷配置的方法</PackageReleaseNotes>
<PackageTags>Alipay,AopSdk,支付宝服务端SDK,支付宝支付</PackageTags>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/stulzq/Alipay.AopSdk.Core</RepositoryUrl>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
141 changes: 2 additions & 139 deletions Alipay.AopSdk.AspnetCore/AlipayConfigChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Security.Cryptography;
using Alipay.AopSdk.Core;
using Alipay.AopSdk.Core.Util;
using Microsoft.Extensions.Options;

namespace Alipay.AopSdk.AspnetCore
Expand All @@ -25,151 +26,13 @@ public static void Check(string signType,string privateKey)
}

//RSA私钥格式检查
RSA rsaCsp = LoadCertificateString(privateKey, signType);
RSA rsaCsp = AlipaySignature.LoadCertificateString(privateKey, signType);

if (rsaCsp == null)
{
throw new Exception("您的支付宝配置未能通过检查,详细信息:商户私钥格式错误,未能导入!");
}

}

private static RSA LoadCertificateString(string strKey, string signType)
{
byte[] data = null;
//读取带
//ata = Encoding.Default.GetBytes(strKey);
data = Convert.FromBase64String(strKey);
//data = GetPem("RSA PRIVATE KEY", data);
try
{
var rsa = DecodeRsaPrivateKey(data, signType);
return rsa;
}
catch (Exception ex)
{
throw new AopException("Alipay.AopSdk.Core.Util.AlipaySignature LoadCertificateString DecodeRSAPrivateKey Error", ex);
}
}

private static RSA DecodeRsaPrivateKey(byte[] privkey, string signType)
{
byte[] MODULUS, E, D, P, Q, DP, DQ, IQ;

// --------- Set up stream to decode the asn.1 encoded RSA private key ------
var mem = new MemoryStream(privkey);
var binr = new BinaryReader(mem); //wrap Memory Stream with BinaryReader for easy reading
byte bt = 0;
ushort twobytes = 0;
var elems = 0;
try
{
twobytes = binr.ReadUInt16();
if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)
binr.ReadByte(); //advance 1 byte
else if (twobytes == 0x8230)
binr.ReadInt16(); //advance 2 bytes
else
return null;

twobytes = binr.ReadUInt16();
if (twobytes != 0x0102) //version number
return null;
bt = binr.ReadByte();
if (bt != 0x00)
return null;


//------ all private key components are Integer sequences ----
elems = GetIntegerSize(binr);
MODULUS = binr.ReadBytes(elems);

elems = GetIntegerSize(binr);
E = binr.ReadBytes(elems);

elems = GetIntegerSize(binr);
D = binr.ReadBytes(elems);

elems = GetIntegerSize(binr);
P = binr.ReadBytes(elems);

elems = GetIntegerSize(binr);
Q = binr.ReadBytes(elems);

elems = GetIntegerSize(binr);
DP = binr.ReadBytes(elems);

elems = GetIntegerSize(binr);
DQ = binr.ReadBytes(elems);

elems = GetIntegerSize(binr);
IQ = binr.ReadBytes(elems);


// ------- create RSACryptoServiceProvider instance and initialize with public key -----
var CspParameters = new CspParameters();
CspParameters.Flags = CspProviderFlags.UseMachineKeyStore;

var bitLen = 1024;
if ("RSA2".Equals(signType))
bitLen = 2048;

var rsa = RSA.Create();
rsa.KeySize = bitLen;
var rsAparams = new RSAParameters();
rsAparams.Modulus = MODULUS;
rsAparams.Exponent = E;
rsAparams.D = D;
rsAparams.P = P;
rsAparams.Q = Q;
rsAparams.DP = DP;
rsAparams.DQ = DQ;
rsAparams.InverseQ = IQ;
rsa.ImportParameters(rsAparams);
return rsa;
}
catch (Exception ex)
{
return null;
}
finally
{
binr.Close();
}
}

private static int GetIntegerSize(BinaryReader binr)
{
byte bt = 0;
byte lowbyte = 0x00;
byte highbyte = 0x00;
var count = 0;
bt = binr.ReadByte();
if (bt != 0x02) //expect integer
return 0;
bt = binr.ReadByte();

if (bt == 0x81)
{
count = binr.ReadByte(); // data size in next byte
}
else if (bt == 0x82)
{
highbyte = binr.ReadByte(); // data size in next 2 bytes
lowbyte = binr.ReadByte();
byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };
count = BitConverter.ToInt32(modint, 0);
}
else
{
count = bt; // we already have the data size
}

while (binr.ReadByte() == 0x00)
//remove high order zeros in data
count -= 1;
binr.BaseStream.Seek(-1, SeekOrigin.Current); //last ReadByte wasn't a removed zero, so back up a byte
return count;
}
}
}
33 changes: 30 additions & 3 deletions Alipay.AopSdk.AspnetCore/AlipayOptions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using Alipay.AopSdk.Core.Domain;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;

namespace Alipay.AopSdk.AspnetCore
{
public class AlipayOptions : IOptions<AlipayOptions>
public class AlipayOptions
{
/// <summary>
/// 应用ID,您的APPID
Expand Down Expand Up @@ -46,6 +47,32 @@ public class AlipayOptions : IOptions<AlipayOptions>
/// </summary>
public bool IsKeyFromFile { get; set; } = false;

AlipayOptions IOptions<AlipayOptions>.Value => this;
}
public void SetOption(IConfigurationSection section)
{
if (section == null)
{
throw new ArgumentException(nameof(section));
}

var options = section.Get<AlipayOptions>();
SetOption(options);
}

public void SetOption(AlipayOptions options)
{
if (options == null)
{
throw new ArgumentException(nameof(options));
}

this.Uid = options.Uid;
this.AlipayPublicKey = options.AlipayPublicKey;
this.AppId = options.AppId;
this.CharSet = options.CharSet;
this.Gatewayurl = options.Gatewayurl;
this.PrivateKey = options.PrivateKey;
this.SignType = options.SignType;
}

}
}
29 changes: 1 addition & 28 deletions Alipay.AopSdk.AspnetCore/AlipayService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,33 +58,6 @@ public bool RSACheckV1(Dictionary<string, string> data)
return AlipaySignature.RSACheckV1(data, Options.AlipayPublicKey, Options.CharSet, Options.SignType, false);
}

/*public Dictionary<string, string> RequestParamToDictionaryForHttpGet()
{
Dictionary<string, string> sArray = new Dictionary<string, string>();
ICollection<string> requestItem = _context.Request.Query.Keys as ICollection<string>;
foreach (var item in requestItem)
{
sArray.Add(item, _context.Request.Query[item]);
}
return sArray;
}
public Dictionary<string, string> RequestParamToDictionaryForHttpPost()
{
Dictionary<string, string> sArray = new Dictionary<string, string>();
ICollection<string> requestItem = _context.Request.Form.Keys as ICollection<string>;
foreach (var item in requestItem)
{
sArray.Add(item, _context.Request.Form[item]);
}
return sArray;
}*/

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ public static IServiceCollection AddAlipay(this IServiceCollection services,Acti
throw new ArgumentNullException(nameof(options));
services.AddOptions();
services.Configure(options);
services.Add(ServiceDescriptor.Transient<IAlipayService, AlipayService>());
// services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddScoped<IAlipayService, AlipayService>();
return services;
}
}
Expand Down
8 changes: 7 additions & 1 deletion Alipay.AopSdk.Core.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Alipay.AopSdk.F2FPay", "Ali
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Alipay.AopSdk.AspnetCore", "Alipay.AopSdk.AspnetCore\Alipay.AopSdk.AspnetCore.csproj", "{6A78B4AB-B857-4BA8-88C4-85F6298C8E70}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Alipay.AopSdk.F2FPay.AspnetCore", "Alipay.AopSdk.F2FPay.AspnetCore\Alipay.AopSdk.F2FPay.AspnetCore.csproj", "{314FA1F7-4545-4C7E-AE12-DC98BF136F27}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Alipay.AopSdk.F2FPay.AspnetCore", "Alipay.AopSdk.F2FPay.AspnetCore\Alipay.AopSdk.F2FPay.AspnetCore.csproj", "{314FA1F7-4545-4C7E-AE12-DC98BF136F27}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{946D3C5A-4580-44C8-9EA9-B529F5657143}"
ProjectSection(SolutionItems) = preProject
LICENSE = LICENSE
README.md = README.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
10 changes: 6 additions & 4 deletions Alipay.AopSdk.Core/Alipay.AopSdk.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>2.2.6</Version>
<Version>2.2.7</Version>
<Authors>stulzq</Authors>
<Description>支付宝(Alipay)服务端SDK,采用.NET Standard 2.0,支持.NET Core 2.0,与官方SDK接口完全相同。完全可以按照官方文档进行开发。除了支持支付以外,官方SDK支持的功能本SDK全部支持,且用法几乎一样,代码都可参考官方文档代码。github:https://github.com/stulzq/Alipay.AopSdk.Core,访问github获取demo以及使用文档。</Description>
<Copyright>Copyright 2017-2018 stulzq</Copyright>
Expand All @@ -11,9 +11,11 @@
<PackageLicenseUrl>https://github.com/stulzq/Alipay.AopSdk.Core/blob/master/LICENSE</PackageLicenseUrl>
<PackageTags>Alipay,AopSdk,支付宝服务端SDK,支付宝支付</PackageTags>
<PackageIconUrl>https://www.alipay.com/favicon.ico</PackageIconUrl>
<PackageReleaseNotes>更新json.net版本</PackageReleaseNotes>
<AssemblyVersion>2.2.6.0</AssemblyVersion>
<FileVersion>2.2.6.0</FileVersion>
<PackageReleaseNotes></PackageReleaseNotes>
<AssemblyVersion>2.2.7.0</AssemblyVersion>
<FileVersion>2.2.7.0</FileVersion>
<RepositoryUrl>https://github.com/stulzq/Alipay.AopSdk.Core</RepositoryUrl>
<RepositoryType>git</RepositoryType>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Alipay.AopSdk.Core/Util/AlipaySignature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ private static RSA LoadCertificateFile(string filename, string signType)
}
}

private static RSA LoadCertificateString(string strKey, string signType)
public static RSA LoadCertificateString(string strKey, string signType)
{
byte[] data = null;
//读取带
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>2.2.6</Version>
<Version>2.2.7</Version>
<Authors>stulzq</Authors>
<Company>stulzq</Company>
<Description>提供扫码支付能力。支付宝(Alipay)服务端SDK AopSdk,当面付ASP.NET Core支持。</Description>
Expand All @@ -11,9 +11,11 @@
<PackageProjectUrl>https://github.com/stulzq/Alipay.AopSdk.Core</PackageProjectUrl>
<PackageIconUrl>https://www.alipay.com/favicon.ico</PackageIconUrl>
<PackageReleaseNotes></PackageReleaseNotes>
<AssemblyVersion>2.2.6.0</AssemblyVersion>
<FileVersion>2.2.6.0</FileVersion>
<AssemblyVersion>2.2.7.0</AssemblyVersion>
<FileVersion>2.2.7.0</FileVersion>
<PackageTags>Alipay,AopSdk,支付宝服务端SDK,支付宝支付</PackageTags>
<RepositoryUrl>https://github.com/stulzq/Alipay.AopSdk.Core</RepositoryUrl>
<RepositoryType>git</RepositoryType>
</PropertyGroup>

<ItemGroup>
Expand Down
10 changes: 6 additions & 4 deletions Alipay.AopSdk.F2FPay/Alipay.AopSdk.F2FPay.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
<PackageProjectUrl>https://github.com/stulzq/Alipay.AopSdk.Core</PackageProjectUrl>
<PackageTags>支付宝当面付,支付宝条码支付,Alipay,AopSdk,支付宝服务端SDK,支付宝支付</PackageTags>
<PackageReleaseNotes></PackageReleaseNotes>
<Description>支付宝当面付SDK,包括条码支付、扫码支付。采用.NET Standard 2.0,支持.NET Core 2.0。github:https://github.com/stulzq/Alipay.AopSdk.Core,访问github获取demo以及使用文档。</Description>
<Description>支付宝当面付SDK,包括条码支付、扫码支付。</Description>
<PackageIconUrl>https://www.alipay.com/favicon.ico</PackageIconUrl>
<AssemblyVersion>2.2.6.0</AssemblyVersion>
<FileVersion>2.2.6.0</FileVersion>
<Version>2.2.6</Version>
<AssemblyVersion>2.2.7.0</AssemblyVersion>
<FileVersion>2.2.7.0</FileVersion>
<Version>2.2.7</Version>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<Copyright>Copyright 2017-2018 stulzq</Copyright>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/stulzq/Alipay.AopSdk.Core</RepositoryUrl>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit fd32b69

Please sign in to comment.