代码:
using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace ADT.TuDou.FineManage { public partial class Atest : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string url = Request.Url.ToString(); string query = Request.Url.Query; if (!string.IsNullOrEmpty(query)) { Dictionary<string, string> dic = new Dictionary<string, string>(); query = query.Substring(1, query.Length - 1); string[] kv = query.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries); foreach (string item in kv) { string[] kv2 = item.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries); dic.Add(kv2[0], kv2[1]); } IDictionary<string, string> sortedParams = new SortedDictionary<string, string>(dic); //dic.OrderBy(c => c.Key).ToDictionary(k => k.Key, v => v.Value); IEnumerator<KeyValuePair<string, string>> iterator = sortedParams.GetEnumerator(); // 遍历排序后的字典,将所有参数按"key=value"格式拼接在一起 StringBuilder basestring = new StringBuilder(); while (iterator.MoveNext()) { string key = iterator.Current.Key; string value = iterator.Current.Value; if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value)) { basestring.Append(key).Append("=").Append(value); } } // 使用MD5对待签名串求签 MD5 md5 = MD5.Create(); byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(basestring.ToString())); // 将MD5输出的二进制结果转换为小写的十六进制 StringBuilder result = new StringBuilder(); for (int i = 0; i < bytes.Length; i++) { string hex = bytes[i].ToString("x"); if (hex.Length == 1) { result.Append("0"); } result.Append(hex); } Response.Write(result.ToString()); } } } }