微信开发模式检验signature asp.net源代码

  微信公众平台要从编辑模式转为开发模式,需要验证自己服务器的url地址和token。

微信开发模式检验signature asp.net源代码

  微信示例demo只提供了php版本的认证

private function checkSignature()
{
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];	
        		
	$token = TOKEN;
	$tmpArr = array($token, $timestamp, $nonce);
	sort($tmpArr, SORT_STRING);
	$tmpStr = implode( $tmpArr );
	$tmpStr = sha1( $tmpStr );
	
	if( $tmpStr == $signature ){
		return true;
	}else{
		return false;
	}
}

  下面为asp.net版本的认证源代码

<%@ WebHandler Language="C#" Class="weixin" %>
using System;
using System.Web;
using System.Web.Security;
using System.Collections.Generic;
public class weixin : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        string s = "";
        List<string> l = new List<string>();
        l.Add("你填写的token");
        l.Add(context.Request.QueryString["timestamp"]);
        l.Add(context.Request.QueryString["nonce"]);
        l.Sort();
        foreach (string _s in l) s += _s;
        s = FormsAuthentication.HashPasswordForStoringInConfigFile(s, "SHA1").ToLower();
        if (s == context.Request.QueryString["signature"])
        {
            context.Response.Write(context.Request.QueryString["echostr"]); 
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }
}

具体参考微信公众平台接入指南

加支付宝好友偷能量挖...


原创文章,转载请注明出处:微信开发模式检验signature asp.net源代码

评论(0)Web开发网
阅读(1210)喜欢(0)Asp.Net/C#/WCF