微信企业号平台开发之回调模式的开启

为了方便把需要加载的参数写在.config中

<!--企业号配置信息-->
<add key="CorpToken" value="Davis"/>
<add key="CorpId" value="wx29410ae4e8dfd0b2"/>
<add key="EncodingAESKey" value="CNphWFJxQXQuSDdgaUHGKfl79KsOxJKum144L6ziyuBvk"/>

其次在一般处理程序中编写代码如下:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using Tencent;

namespace Wechat.Config
{
/// <summary>
/// Summary description for QYService
/// </summary>
public class QYService : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
string postString = string.Empty;
if (HttpContext.Current.Request.HttpMethod.ToUpper() == "GET")
{
Auth();
}
}

public bool IsReusable
{
get
{
return false;
}
}

/// <summary>
/// 成为开发者的第一步,验证并相应服务器的数据
/// </summary>
private void Auth()
{
#region 获取关键参数
string token = ConfigurationManager.AppSettings["CorpToken"];//从配置文件获取Token
if (string.IsNullOrEmpty(token))
{
//LogTextHelper.Error(string.Format("CorpToken 配置项没有配置!"));
}
string encodingAESKey = ConfigurationManager.AppSettings["EncodingAESKey"];//从配置文件获取EncodingAESKey
if (string.IsNullOrEmpty(encodingAESKey))
{
//LogTextHelper.Error(string.Format("EncodingAESKey 配置项没有配置!"));
}
string corpId = ConfigurationManager.AppSettings["CorpId"];//从配置文件获取corpId
if (string.IsNullOrEmpty(corpId))
{
//LogTextHelper.Error(string.Format("CorpId 配置项没有配置!"));
}
#endregion

string echoString = HttpContext.Current.Request.QueryString["echoStr"];
string signature = HttpContext.Current.Request.QueryString["msg_signature"];//企业号的 msg_signature
string timestamp = HttpContext.Current.Request.QueryString["timestamp"];
string nonce = HttpContext.Current.Request.QueryString["nonce"];

string decryptEchoString = "";
if (CheckSignature(token, signature, timestamp, nonce, corpId, encodingAESKey, echoString, ref decryptEchoString))
{
if (!string.IsNullOrEmpty(decryptEchoString))
{
HttpContext.Current.Response.Write(decryptEchoString);
HttpContext.Current.Response.End();
}
}
}

/// <summary>
/// 验证企业号签名
/// </summary>
/// <param name="token">企业号配置的Token</param>
/// <param name="signature">签名内容</param>
/// <param name="timestamp">时间戳</param>
/// <param name="nonce">nonce参数</param>
/// <param name="corpId">企业号ID标识</param>
/// <param name="encodingAESKey">加密键</param>
/// <param name="echostr">内容字符串</param>
/// <param name="retEchostr">返回的字符串</param>
/// <returns></returns>
public bool CheckSignature(string token, string signature, string timestamp, string nonce, string corpId, string encodingAESKey, string echostr, ref string retEchostr)
{
WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(token, encodingAESKey, corpId);
int result = wxcpt.VerifyURL(signature, timestamp, nonce, echostr, ref retEchostr);
if (result != 0)
{
//LogTextHelper.Error("ERR: VerifyURL fail, ret: " + result);
return false;
}

return true;

//ret==0表示验证成功,retEchostr参数表示明文,用户需要将retEchostr作为get请求的返回参数,返回给企业号。
// HttpUtils.SetResponse(retEchostr);
}
}
}

然后在企业号回调函数中配置中输入:

本文资源来源于网络牛人,具体忘了。

原文地址:https://www.cnblogs.com/jason-davis/p/5231268.html