zoukankan      html  css  js  c++  java
  • 基于C#的微信公众平台开发系列1

    1.首先服务器地址及Token验证:

    Token验证请求地址wx.ashx代码:

     1 using System;
     2 using System.Web;
     3 
     4 public class wx : IHttpHandler {
     5     
     6     public void ProcessRequest (HttpContext context) 
     7     {
     8         context.Response.ContentType = "text/plain";
     9         //
    10         if (context.Request.HttpMethod.ToLower().Equals("get"))
    11         {
    12             ValidateUrl();
    13         }
    14         else {
    15             context.Response.Write("wgx:post");
    16         }
    17     }
    18    #region 服务器地址校验
    19     /// <summary>
    20     /// 服务器地址校验
    21     /// </summary>
    22     public void ValidateUrl()
    23     {
    24         /* signature    微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
    25             timestamp    时间戳
    26             nonce    随机数
    27             echostr    随机字符串
    28         */
    29         //接收请求过来的参数
    30         HttpContext context = HttpContext.Current;
    31         string signature = context.Request["signature"];
    32         string timestamp = context.Request["timestamp"];
    33         string nonce = context.Request["nonce"];
    34 
    35         string echostr = context.Request["echostr"];
    36         
    37         string token = "weixinduang";
    38         string[] temp1 = { token, timestamp, nonce };
    39         //1.字典序排序
    40         Array.Sort(temp1);
    41         //2.将3个参数字符串拼接成一个字符串后进行sha1加密
    42         string str = string.Join("", temp1);
    43         string strResult = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "SHA1");
    44         //3.开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
    45         if (strResult.ToLower().Equals(signature))
    46         {
    47             context.Response.Write(echostr);
    48         }
    49     } 
    50     #endregion
    51         
    52     public bool IsReusable {
    53         get {
    54             return false;
    55         }
    56     }
    57 }

    如果验证通过会提示成功,否则失败。

  • 相关阅读:
    python 递归一行实现字符串反转
    HABSE安装教程
    Target runtime Apache Tomcat v7.0 is not defined.
    论各种非人性化自动设置及关闭位置(持续更新中。。。)
    装饰者模式
    傻瓜式servlet监听器简单实例
    editplus代码格式化
    session,cookie机制
    servlet文件部署在tomcat上
    python学习笔记(一):作图
  • 原文地址:https://www.cnblogs.com/wgx0428/p/4319335.html
Copyright © 2011-2022 走看看