微信开发第一步 : 配置API,实现接收消息服务配置
1.在下图界面先填好内容,事件消息处理可自行选择,我这里是没处理的

2.第二步: 使用vs 进行代码的编写,以下是我的代码。LogTextHelper类需要自己写的,微信官方文档下载地址 https://work.weixin.qq.com/api/doc#10128 ,里面有多种版本,可自行选择。
/// <summary>
/// 处理企业号的信息
/// </summary>
/// <param name="context"></param>
public void ProcessRequest(HttpContext context)
{
byte[] btHtml = context.Request.BinaryRead(context.Request.ContentLength);
string strHtml = System.Text.Encoding.Default.GetString(btHtml);
LogTextHelper.WriteLogFile(context.Request.Url.ToString() + "\r\n" + strHtml);
string postString = string.Empty;
if (context.Request.HttpMethod.ToUpper() == "GET")
{
Auth();
}
//POST
}
/// <summary>
/// 成为开发者的第一步,验证并相应服务器的数据
/// </summary>
private void Auth()
{
LogTextHelper LogHelper = new LogTextHelper();
string token = ConfigurationManager.AppSettings["CorpToken"];//从配置文件获取Token
string encodingAESKey = ConfigurationManager.AppSettings["EncodingAESKey"];//从配置文件获取EncodingAESKey
string corpId = ConfigurationManager.AppSettings["CorpId"];//从配置文件获取corpId
string echoStringinitial = HttpContext.Current.Request.QueryString["echostr"];
string echoString = HttpUtility.UrlDecode(echoStringinitial); // 开发文档中写的“企业在获取时需要做urldecode处理”
string signature = HttpContext.Current.Request.QueryString["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 ret=wxcpt.VerifyURL(signature,timestamp,nonce,echostr,ref retEchostr);
if (ret != 0)
{
//LogTextHelper.Error("ERR: VerifyURL fail, ret: " + ret);
return false;
}
// HttpContext.Current.Response.Write(retEchostr);
return true;
//ret==0表示验证成功,retEchostr参数表示明文,用户需要将retEchostr作为get请求的返回参数,返回给企业号。
//HttpUtils.SetResponse(retEchostr);
}
public bool IsReusable
{
get
{
return false;
}
}
}
3.第三步: 导入微信官方加解密文件,之后注意 token ,corpid ,encodingAESKey 的值保持一致 。 还有一点就是要注意,cshap 库要下载最新版,老版是没有 VerifyURL() 这个方法的。 这是下载地址 : https://work.weixin.qq.com/api/doc#10128
<appSettings>
<!--企业号配置信息-->
<add key="CorpToken" value="自己所填的token"/>
<add key="CorpId" value="自己企业号的corpid"/>
<add key="EncodingAESKey" value="自己所填的加密键"/>
</appSettings>
4.将所写的 web应用程序发布到 FTP 上 ,在回到 服务器配置那 ,点击保存,第一次点击会出现 回调URL失败 ,这是因为值还没传过来 。 之后你写的日志文件里就会多个Log ,将里面的值返回到web应用程序里。 如下图:

这微信服务器发过来的参数 ,Log 里面有。

5.这是LogTextHelper.cs 类
public class LogTextHelper { // 创建日志 ///----------------------------------------------------------------------------- /// <summary>创建错误日志 在c:\ErrorLog\</summary> /// <param name="message">记录信息 /// <returns></returns> ///----------------------------------------------------------------------------- public static void WriteLogFile(string message) { string strPath; //文件的路径 DateTime dt = DateTime.Now; try { strPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Log"); //winform工程\bin\目录下 创建日志文件夹 if (Directory.Exists(strPath) == false) //工程目录下 Log目录 '目录是否存在,为true则没有此目录 { Directory.CreateDirectory(strPath); //建立目录 Directory为目录对象 } strPath = strPath + "\\" + dt.ToString("yyyy"); if (Directory.Exists(strPath) == false) { Directory.CreateDirectory(strPath); } strPath = strPath + "\\" + dt.Year.ToString() + "-" + dt.Month.ToString() + ".txt"; StreamWriter FileWriter = new StreamWriter(strPath, true); //创建日志文件 FileWriter.WriteLine("[" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "] " + message); FileWriter.Close(); //关闭StreamWriter对象 } catch (Exception ex) { string str = ex.Message.ToString(); } }
