實際案例: 接口接入驗證 (程式碼)

同樣的

本案例也是採用大名鼎鼎的 Senparc 微信開發套件

廢話不多說,具體程式碼如下

using ShouJia.BO;
using ShouJia.Common;
using ShouJia.Debugger;
using ShouJia.Facades;
using ShouJia.Framework;
using Senparc.Weixin.MP;
using System;
using System.Data;
using System.Data.Common;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using System.Web;


namespace ShouJia.VXinWeb
{
    /// <summary>
    /// WxAuth 的摘要描述
    /// </summary>
    public class WxAuth : IHttpHandler
    {
        private string Token = string.Empty;
        private int GarageID;

        public void ProcessRequest(HttpContext c)
        {
            
            GarageID = int.Parse(CommonHelper.RequestParam("GarageID"));
            WebLogs.Debug( "API Auth Get GarageID", GarageID.ToString() );

            Token = WeixinBehaviors.GetApiAuthToken(GarageID);
            WebLogs.Debug( "API Auth Get Token", Token );

            string signature = CommonHelper.RequestParam("signature");
            WebLogs.Debug( "API Auth Get signature", signature );

            string timestamp = CommonHelper.RequestParam("timestamp");
            WebLogs.Debug( "API Auth Get timestamp", timestamp );

            string nonce = CommonHelper.RequestParam("nonce");
            WebLogs.Debug( "API Auth Get nonce", nonce );

            string echostr = CommonHelper.RequestParam("echostr");
            WebLogs.Debug( "API Auth Get echostr", echostr );

            if ( HttpContext.Current.Request.HttpMethod.ToUpper() == "GET" )
            {
                // get method - 仅在微信后台填写URL验证时触发
                if ( CheckSignature.Check( signature, timestamp, nonce, Token ) )
                {
                    WriteContent(echostr); //返回随机字符串则表示验证通过
                }
                else
                {
                    WriteContent("failed:" + signature + "," + CheckSignature.GetSignature(timestamp, nonce, Token) + "。如果你在浏览器中看到这句话,说明此地址可以被作为微信公众账号后台的Url,请注意保持Token一致。");
                }

                HttpContext.Current.Response.End();
            }
            else
            {
                // post method - 当有用户想公众账号发送消息时触发
                if ( !CheckSignature.Check( signature, timestamp, nonce, Token ) )
                {
                    WriteContent("参数错误!");
                    return;
                }

                using (Stream s = HttpContext.Current.Request.InputStream)
                {
                    Byte[] postBytes = new Byte[s.Length];
                    s.Read(postBytes, 0, (Int32)s.Length);
                    string postStr = Encoding.UTF8.GetString(postBytes);
                    var h = new Tools.CustomMessageHandler();
                    h.ResponseXmlData(postStr);
                }
            }
        }

        private void WriteContent(string output_str)
        {
            HttpContext.Current.Response.Write(output_str);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

 

转载于:https://www.cnblogs.com/hsw1976/p/5481414.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值