微网站公众平台开发:怎么做微接入验证
发布人:桔子科技 发布时间:2015-01-05 09:54:49 分享到:
相要通过微和自身业务进行紧密相连,就需要实现如何与微服务器进行对接,涉足
微开发,首先需要到微公众平台申请一个微公众号,打开开发者模式后,首先需要对您填写的url和
Token进行验证,验证url的有效性,是微后续开发的首要条件。下面是实现url验证判断的代码:
///
/// 微URL验证
///
private void RenZheng()
{
#region 新认证
if (Request.HttpMethod.ToUpper() == "GET")
{
// 微加密签名
string signature = Request.QueryString["signature"];
// 时间戳
string timestamp = Request.QueryString["timestamp"];
// 随机数
string nonce = Request.QueryString["nonce"];
// 随机字符串
string echostr = Request.QueryString["echostr"];
if (CheckSignature(signature, timestamp, nonce))
{
Response.Write(echostr);
Response.End();
}
}
#endregion
}
public static bool CheckSignature(String signature, Stringtimestamp, String nonce)
{
string token = ViewUtil.WXToken;
String[] arr = new String[] { token, timestamp, nonce };
// 将token、timestamp、nonce三个参数进行字典序排序
Array.Sort(arr);
StringBuilder content = new StringBuilder();
for (int i = 0; i < arr.Length; i++)
{
content.Append(arr[i]);
}
String tmpStr = SHA1_Encrypt(content.ToString());
// 将sha1加密后的字符串可与signature对比,标识该请求来源于微
return tmpStr != null ? tmpStr.Equals(signature) : false;
}
///
/// 使用缺省密钥给字符串加密
///
///
///
public static string SHA1_Encrypt(string Source_String)
{
byte[] StrRes = Encoding.Default.GetBytes(Source_String);
HashAlgorithm iSHA = new SHA1CryptoServiceProvider();
StrRes = iSHA.ComputeHash(StrRes);
StringBuilder EnText = new StringBuilder();
foreach (byte iByte in StrRes)
{
EnText.AppendFormat("{0:x2}", iByte);
}
return EnText.ToString();
}
}
验证成功后,即可成为开发者,可以根据自身业务实现各种个性话的功能定制,比如智能回复,多客服,消息自动推送,位置定位等,多样化的功能使得自身系统变得更加贴近实用,为用户带来实实在在的益处!
下一篇:建设微网站对企业有哪些好处呢?上一篇:微网站能给传统企业带来什么