/// <summary> /// 生成要请求给支付宝的参数数组 /// </summary> /// <param name="sParaTemp">请求前的参数数组</param> /// <returns>要请求的参数数组</returns> private static Dictionary <string, string> BuildRequestPara(Dictionary <string, string> sParaTemp) { //待签名请求参数数组 Dictionary <string, string> sPara = new Dictionary <string, string>(); //签名结果 string mysign = ""; //过滤签名参数数组 sPara = AlipayCore.FilterPara(sParaTemp); //根据字母a到z的顺序把参数排序 sPara = AlipayCore.SortPara(sPara); //获得签名结果 mysign = BuildRequestMysign(sPara); //签名结果与签名方式加入请求提交参数组中 sPara.Add("sign", mysign); if (sPara["service"] != "alipay.wap.trade.create.direct" && sPara["service"] != "alipay.wap.auth.authAndExecute") { sPara.Add("sign_type", _sign_type); } return(sPara); }
/// <summary> /// 获取返回时的签名验证结果 /// </summary> /// <param name="inputPara">通知返回参数数组</param> /// <param name="sign">对比的签名结果</param> /// <param name="isSort">是否对待签名数组排序</param> /// <returns>签名验证结果</returns> private bool GetSignVeryfy(Dictionary <string, string> inputPara, string sign, bool isSort) { BaseService.WriteLog("RSA获取返回时的签名验证结果: AlipayNotify.GetSignVeryfy()"); BaseService.WriteLog("sign: " + sign); BaseService.WriteLog("isSort: " + isSort); Dictionary <string, string> sPara = new Dictionary <string, string>(); //过滤空值、sign与sign_type参数 sPara = AlipayCore.FilterPara(inputPara); if (isSort) { //根据字母a到z的顺序把参数排序 sPara = AlipayCore.SortPara(sPara); BaseService.WriteLog("排序后 sPara: " + sPara.ToString()); } //获取待签名字符串 string preSignStr = AlipayCore.CreateLinkString(sPara); BaseService.WriteLog("preSignStr: " + preSignStr); //获得签名验证结果 bool isSgin = false; if (sign != null && sign != "") { BaseService.WriteLog("_sign_type: " + _sign_type); switch (_sign_type) { case "MD5": isSgin = AlipayMD5.Verify(preSignStr, sign, _key, _input_charset); break; case "RSA": isSgin = RSAFromPkcs8.verify(preSignStr, sign, _public_key, _input_charset); break; case "0001": isSgin = RSAFromPkcs8.verify(preSignStr, sign, _public_key, _input_charset); break; default: break; } } BaseService.WriteLog("isSgin: " + isSgin); return(isSgin); }
/// <summary> /// 获取待签名字符串(调试用) /// </summary> /// <param name="inputPara">通知返回参数数组</param> /// <returns>待签名字符串</returns> public string GetPreSignStr(Dictionary <string, string> inputPara) { Dictionary <string, string> sPara = new Dictionary <string, string>(); //过滤空值、sign与sign_type参数 sPara = AlipayCore.FilterPara(inputPara); //根据字母a到z的顺序把参数排序 sPara = AlipayCore.SortPara(sPara); //获取待签名字符串 string preSignStr = AlipayCore.CreateLinkString(sPara); return(preSignStr); }