141 lines
4.7 KiB
C#
141 lines
4.7 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Web;
|
||
using System.Security.Cryptography;
|
||
using System.Text;
|
||
using System.Xml;
|
||
|
||
namespace Mtxfw.Utility
|
||
{
|
||
public class Interface_WxeditAddress
|
||
{
|
||
|
||
public static string Get_RequestHtml(HttpContext Context, string APPID,string accessToken, string webUrl)
|
||
{
|
||
|
||
//设置package订单参数
|
||
SortedDictionary<string, string> dic = new SortedDictionary<string, string>();
|
||
|
||
string wx_nonceStr = getNoncestr();
|
||
dic.Add("url", webUrl);
|
||
dic.Add("appid", APPID);
|
||
dic.Add("timestamp", getTimestamp());
|
||
dic.Add("noncestr", wx_nonceStr);
|
||
dic.Add("accesstoken", accessToken);
|
||
|
||
string get_sign = BuildRequest(dic);
|
||
|
||
return "\"appId\" : \"" + APPID + "\",\"scope\" : \"jsapi_address\",\"signType\" : \"SHA1\",\"addrSign\" : \"" + get_sign + "\",\"timeStamp\" : \"" + getTimestamp() + "\",\"nonceStr\" : \"" + wx_nonceStr + "\"";
|
||
}
|
||
public static string getNoncestr()
|
||
{
|
||
Random random = new Random();
|
||
return GetMD5(random.Next(1000).ToString(), "utf-8");
|
||
}
|
||
|
||
public static string getTimestamp()
|
||
{
|
||
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
|
||
return Convert.ToInt64(ts.TotalSeconds).ToString();
|
||
}
|
||
|
||
public static string BuildRequest(SortedDictionary<string, string> sParaTemp)
|
||
{
|
||
//获取过滤后的数组
|
||
Dictionary<string, string> dicPara = new Dictionary<string, string>();
|
||
dicPara = FilterPara(sParaTemp);
|
||
|
||
//组合参数数组
|
||
string prestr = CreateLinkString(dicPara);
|
||
//拼接支付密钥
|
||
string stringSignTemp = prestr;
|
||
|
||
//获得加密结果
|
||
string mySign = GetSHA(stringSignTemp);
|
||
|
||
//返回转换为大写的加密串
|
||
return mySign;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 除去数组中的空值和签名参数并以字母a到z的顺序排序
|
||
/// </summary>
|
||
/// <param name="dicArrayPre">过滤前的参数组</param>
|
||
/// <returns>过滤后的参数组</returns>
|
||
public static Dictionary<string, string> FilterPara(SortedDictionary<string, string> dicArrayPre)
|
||
{
|
||
Dictionary<string, string> dicArray = new Dictionary<string, string>();
|
||
foreach (KeyValuePair<string, string> temp in dicArrayPre)
|
||
{
|
||
if (temp.Key != "sign" && !string.IsNullOrEmpty(temp.Value))
|
||
{
|
||
dicArray.Add(temp.Key, temp.Value);
|
||
}
|
||
}
|
||
|
||
return dicArray;
|
||
}
|
||
|
||
//组合参数数组
|
||
public static string CreateLinkString(Dictionary<string, string> dicArray)
|
||
{
|
||
StringBuilder prestr = new StringBuilder();
|
||
foreach (KeyValuePair<string, string> temp in dicArray)
|
||
{
|
||
prestr.Append(temp.Key + "=" + temp.Value + "&");
|
||
}
|
||
|
||
int nLen = prestr.Length;
|
||
prestr.Remove(nLen - 1, 1);
|
||
|
||
return prestr.ToString();
|
||
}
|
||
public static string GetSHA(string pwd)
|
||
{
|
||
string tmpStr = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, "SHA1");
|
||
return tmpStr;
|
||
}
|
||
//加密
|
||
public static string GetMD5(string pwd)
|
||
{
|
||
MD5 md5Hasher = MD5.Create();
|
||
|
||
byte[] data = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(pwd));
|
||
|
||
StringBuilder sBuilder = new StringBuilder();
|
||
for (int i = 0; i < data.Length; i++)
|
||
{
|
||
sBuilder.Append(data[i].ToString("x2"));
|
||
}
|
||
|
||
return sBuilder.ToString();
|
||
}
|
||
|
||
/** 获取大写的MD5签名结果 */
|
||
public static string GetMD5(string encypStr, string charset)
|
||
{
|
||
string retStr;
|
||
MD5CryptoServiceProvider m5 = new MD5CryptoServiceProvider();
|
||
|
||
//创建md5对象
|
||
byte[] inputBye;
|
||
byte[] outputBye;
|
||
|
||
//使用GB2312编码方式把字符串转化为字节数组.
|
||
try
|
||
{
|
||
inputBye = Encoding.GetEncoding(charset).GetBytes(encypStr);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
inputBye = Encoding.GetEncoding("GB2312").GetBytes(encypStr);
|
||
}
|
||
outputBye = m5.ComputeHash(inputBye);
|
||
|
||
retStr = System.BitConverter.ToString(outputBye);
|
||
retStr = retStr.Replace("-", "").ToUpper();
|
||
return retStr;
|
||
}
|
||
}
|
||
} |