童鞋们直接看代码吧:(我这里是ashx处理程序写的类,开发过网站的一般都知道)

<%@ WebHandler Language="C#" class="weixin" %>

using System;
using System.Web;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Security.Policy;
using System.Collections;
using System.Xml;

public class weixin : IHttpHandler {
    protected string TOKEN = "asdasd"; //TOKEN 必须跟你在微信公众平台上写的token是一致的
    public void ProcessRequest (HttpContext context) {
        //context.Response.ContentType = "text/plain";
        context.Response.Clear(); //清除所有之前生成的Response内容
        Handlewinxin(context);  //进入专业微信处理程序
        context.Response.End();
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }
    /// <summary>
    ///
    /// </summary>
    /// <param name="content"></param>
    public void Handlewinxin(HttpContext context)
    {
        
        if (context.Request.HttpMethod.ToUpper() == "GET")
        {
            // 微信加密签名  
            string signature = context.Request.QueryString["signature"];
            // 时间戳  
            string timestamp = context.Request.QueryString["timestamp"];
            // 随机数  
            string nonce = context.Request.QueryString["nonce"];
            // 随机字符串  
            string echostr = context.Request.QueryString["echostr"];
            if (CheckSignature(signature, timestamp, nonce))
            {
                context.Response.Write(echostr);
            }
            
        }
        else if (context.Request.HttpMethod.ToUpper() == "POST")
        {

StreamReader stream = new StreamReader(context.Request.InputStream);
            string xml = stream.ReadToEnd();

processRequest(xml, context);
        }
    }
    /// <summary>
    /// 验证签名
    /// </summary>
    /// <param name="signature"></param>
    /// <param name="timestamp"></param>
    /// <param name="nonce"></param>
    /// <returns></returns>
    public bool CheckSignature(String signature, String timestamp, String nonce)
    {
        String[] arr = new String[] { TOKEN, timestamp, nonce };
        // 将token、timestamp、nonce三个参数进行字典序排序  
        Array.Sort<String>(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;
    }

/// <summary>
    /// 使用缺省密钥给字符串加密
    /// </summary>
    /// <param name="Source_String"></param>
    /// <returns></returns>
    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();
    }
    /// <summary>
    /// 处理微信发来的请求
    /// </summary>
    /// <param name="xml"></param>
    public void processRequest(String xml,HttpContext context)
    {
        //待下一章节全部贴出代码
    }

}

最新文章

  1. 利用SkyDrive Pro 迅速批量下载SharePoint Server 上已上传的文件
  2. 第24章 java线程(3)-线程的生命周期
  3. C#_实用
  4. Sony Z1 flashtool 刷机笔记
  5. Distance Between Points
  6. cocos2d-x之Box2d初试
  7. [转载] c++ cout 格式化输出浮点数、整数及格方法
  8. Yii源码阅读笔记(十四)
  9. EF6 在原有数据库中使用 CodeFirst 总复习(五、生成发帖页面)
  10. hadoop 配置文件注意问题
  11. 通常编译亲测56Y国际象棋源代码,精仿56Y国际象棋完整的源代码下载!
  12. Java的内存泄漏
  13. Linux下服务器重启
  14. android开发第一天
  15. 【机器学习实战】第12章 使用FP-growth算法来高效发现频繁项集
  16. C#中抽象类和接口的区别2
  17. UML作业第三次:分析《书店图书销售管理系统,绘制类图
  18. RN 使用第三方字体
  19. eclipse中启动项目报内存溢出问题通过修改配置解决
  20. switch case语句中能否作用在String,long上

热门文章

  1. 自定义Attribute 服务端校验 客户端校验
  2. MyEclipse 选中属性或方法后 相同的不变色了?
  3. Check Box Select/Deselect All on Grid
  4. php header 函数详解
  5. HDFS文件读写流程
  6. html中的框架
  7. delphi 基础之三 文件流操作
  8. PHP搜索MYSQL数据库加分页浏览小结
  9. ORACLE DG之参数详解
  10. 第一步 django的下载安装