第一步:填写服务器配置
第二步:验证消息的确来自微信服务器
开发者提交信息后,微信服务器将发送GET请求到填写的服务器地址URL上,GET请求携带参数如下表所示:
参数 | 描述 |
signature | 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。 |
timestamp | 时间戳 |
nonce | 随机数 |
echostr | 随机字符串 |
开发者通过检验signature对请求进行校验(下面有校验方式)。若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败。加密/校验流程如下:
1)将token、timestamp、nonce三个参数进行字典序排序
2)将三个参数字符串拼接成一个字符串进行sha1加密
3)开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
这部分要通过开发实现
在后台xx.cs文件里面写
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;
using System.Web.Security;
namespace WebApplication4
{
public partial class WebForm12 : System.Web.UI.Page
{
const string AppUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
const string postUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=";
const string postDelUrl = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token="; //删除菜单的URL
const string AppID = "wx72d62513adab1163";
const string AppSecret = "e479a6d6a4cffb7ab20380182ecc16f2";
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
//这段代码主要验证有没有跟微信建立通信,通过本地目录创建一个记事本
//string file = Server.MapPath("test.txt");
//string content = "asdfsafd111--";
//if (File.Exists(file) == true)
//{
// FileStream myFs = new FileStream(file, FileMode.Open);
// StreamWriter mySw = new StreamWriter(myFs);
// mySw.Write(content); mySw.Close(); myFs.Close();
//}
//else
//{
// FileStream myFs = new FileStream(file, FileMode.Create);
// StreamWriter mySw = new StreamWriter(myFs);
// mySw.Write(content); mySw.Close(); myFs.Close();
//}
//如果是get直接执行Valid()函数
Valid();
}
// <summary>
// 验证微信签名
// </summary>
// * 将token、timestamp、nonce三个参数进行字典序排序
// * 将三个参数字符串拼接成一个字符串进行sha1加密
// * 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。
// <returns></returns>
private void Valid()
{
string echoStr = HttpContext.Current.Request.QueryString["echoStr"].ToString();
if (CheckSignature())
{
if (!string.IsNullOrEmpty(echoStr))
{
Response.Write(echoStr);
Response.End();
}
}
}
private bool CheckSignature()
{
string signature = HttpContext.Current.Request.QueryString["signature"].ToString();
string timestamp = HttpContext.Current.Request.QueryString["timestamp"].ToString();
string nonce = HttpContext.Current.Request.QueryString["nonce"].ToString();
string Token = "xxxxxx";//你的token 非常重要
string[] ArrTmp = { Token, timestamp, nonce };
Array.Sort(ArrTmp); //字典排序
string tmpStr = string.Join("", ArrTmp);
tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
tmpStr = tmpStr.ToLower();
if (tmpStr == signature)
{
return true;
}
else
{
return false;
}
}
}
}
第三步:通过POST方法创建自定义菜单
在xx.aspx中添加按钮,通过这个按钮响应的post
<asp:Button ID="postWeixin" runat="server"
CssClass="logincn" onclick="postWeixin_Click" ></asp:Button>&
在后台xx.cs贴出完整代码
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;
using System.Web.Security;
namespace WebApplication4
{
public partial class WebForm12 : System.Web.UI.Page
{
const string AppUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
const string postUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=";
const string postDelUrl = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token="; //删除菜单的URL
const string AppID = "wx72d62513adab1163";
const string AppSecret = "e479a6d6a4cffb7ab20380182ecc16f2";
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
}
protected void postWeixin_Click(object sender, EventArgs e)
{
string menuJson = GetWXMenuStr();
PostMenuData(postUrl + GetAccessToken(), menuJson);
Response.Write("<script language=javascript>alert('创建成功!')</script>");
}
private static string GetAccessToken()
{
WebClient webClient = new WebClient();
Byte[] bytes = webClient.DownloadData(string.Format("{0}&appid={1}&secret={2}", AppUrl, AppID, AppSecret));
string result = Encoding.GetEncoding("utf-8").GetString(bytes);
//JObject jObj = JObject.Parse(result);
//JToken token = jObj["access_token"];
//return token.ToString().Substring(1, token.ToString().Length - 2);
string[] temp = result.Split(',');
string[] tp = temp[0].Split(':');
return tp[1].ToString().Replace('"', ' ').Trim().ToString();
}
private string GetWXMenuStr()
{
string weixin1 = "";
weixin1 += "{\n";
weixin1 += "\"button\":[\n";
weixin1 += "{\n";
// weixin1 += "\"type\":\"click\",\n";
//第一个菜单
weixin1 += "\"name\":\"公共信息\",\n";
weixin1 += "\"sub_button\":[\n";
weixin1 += "{\n";
weixin1 += "\"type\":\"click\",\n";
weixin1 += "\"name\":\"通知公告\",\n";
weixin1 += "\"key\":\"15\"\n";
weixin1 += "}]\n";
weixin1 += "},\n";
//第二个菜单(view类型的)
weixin1 += "{\n";
weixin1 += "\"name\":\"互动交流\",\n";
weixin1 += "\"sub_button\":[\n";
weixin1 += "{\n";
weixin1 += "\"type\":\"view\",\n";
weixin1 += "\"name\":\"菜单描述\",\n";
weixin1 += "\"url\":\"http://www.xxxx.xxxx.cn/xxxxxxlogin.aspx\"\n";
weixin1 += "},\n";
weixin1 += "{\n";
weixin1 += "\"type\":\"view\",\n";
weixin1 += "\"name\":\"官网\",\n";
weixin1 += "\"url\":\"http://www.xxxxx.com.cn\"\n";
weixin1 += "}]\n";
weixin1 += "}\n";
weixin1 += "}]\n";
weixin1 += "}\n";
return weixin1;
}
private void PostMenuData(string url, string postData)
{
Stream outstream = null;
Stream instream = null;
StreamReader sr = null;
HttpWebResponse response = null;
HttpWebRequest request = null;
Encoding encoding = Encoding.UTF8;
byte[] data = encoding.GetBytes(postData);
// 准备请求...
try
{
// 设置参数
request = WebRequest.Create(url) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
outstream = request.GetRequestStream();
outstream.Write(data, 0, data.Length);
outstream.Close();
//发送请求并获取相应回应数据
response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
instream = response.GetResponseStream();
sr = new StreamReader(instream, encoding);
//返回结果网页(html)代码
string content = sr.ReadToEnd();
string err = string.Empty;
// return content;
}
catch (Exception ex)
{
string err = ex.Message;
//return string.Empty;
}
}
}
}