|
上面有完整的源代码:
我把获取js-sdk签名的代码复制过来了,代码如下:
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- namespace WeChat.BLL
- {
- public class WeChatSign
- {
- /// <summary>
- /// JS-SDK签名
- /// </summary>
- /// <param name="url"></param>
- /// <param name="noncestr"></param>
- /// <param name="time"></param>
- /// <returns></returns>
- public static string Sign(string url,string noncestr,string time)
- {
- Dictionary<string, string> dic = new Dictionary<string, string>();
- dic.Add("noncestr", noncestr);
- dic.Add("jsapi_ticket", Config.JSAPI_TICKET);
- dic.Add("timestamp", time);
- dic.Add("url", url);
- ArrayList akeys = new ArrayList(dic.Keys);
- akeys.Sort();
- StringBuilder sb = new StringBuilder();
- foreach (string k in akeys)
- {
- sb.Append(k + "=" + dic[k] + "&");
- }
- //去掉最后一个符号
- sb.Remove(sb.Length - 1, 1);
- return SHA1(sb.ToString()).ToLower();
- }
- /// <summary>
- /// sign签名
- /// </summary>
- /// <param name="dic"></param>
- /// <returns></returns>
- public static string Sign(string[] dic)
- {
- //将token、timestamp、nonce三个参数进行字典序排序
- Array.Sort(dic);//字典排序
- string tmpStr = string.Join("", dic);
- return SHA1(tmpStr).ToLower();
- }
- /// <summary>
- /// sha1加密
- /// </summary>
- /// <param name="text"></param>
- /// <returns></returns>
- private static string SHA1(string text)
- {
- byte[] cleanBytes = Encoding.Default.GetBytes(text);
- byte[] hashedBytes = System.Security.Cryptography.SHA1.Create().ComputeHash(cleanBytes);
- return BitConverter.ToString(hashedBytes).Replace("-", "");
- }
- }
- }
复制代码
|
上一篇:关于“码农网”的简介下一篇:c# 可空类型转换为非空类型
|