微信公众号接口服务器认证配置

移动端的项目之前一直没有接触过,最近做了一个公众号的项目。做过公众号的程序猿都清楚,开发公众号需要访问后台接口服务器。首先通过公众号接口测试号登录微信公众平台

URL地址是外网接口服务器地址。Token是自定义的一个字符串,验证接口需要用到的参数。本人是做C# 开发,所以通过IIS配置了一个站点服务器。

void context_startRequest(object sender, EventArgs e)
{

     HttpApplication app = sender as HttpApplication;
     HttpContext context = app.Context;
     System.Web.HttpRequest Request = context.Request;
     string signature = Request["signature"];
     string timestamp = Request["timestamp"];
     string nonce = Request["nonce"];
     string echostr = Request["echostr"];
     string token = "fdfdfgjh785558943";

     if (Request.HttpMethod == "GET")
        {
          //get method - 仅在微信后台填写URL验证时触发
          if (CheckSignature(token, signature, timestamp, nonce))
             {
                 context.Response.Write(echostr); //返回随机字符串则表示验证通过
             }
          else
             {
                 context.Response.Write("failed:" + signature);
             }
          context.Response.End();
        }
}

private static bool CheckSignature(string token, string signature, string timestamp, string nonce)
{
     string[] ArrTmp = { token, timestamp, nonce };
     //字典排序
     Array.Sort(ArrTmp);
     //拼接
     string tmpStr = string.Join("", ArrTmp);
     //sha1验证
     tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
     tmpStr = tmpStr.ToLower();
     if (tmpStr == signature)
     {
         return true;
     }
     else
     {
         return false;
     }
}

上面这段代码是我通过添加HttpModel文件,在context_startRequest事件中进行的公众号服务器验证处理。希望对初学公众号开发的朋友有所帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旭日升的博客园

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值