首页 >> js开发 >> js微信公众号服务器验证Token步骤图解js大全
js微信公众号服务器验证Token步骤图解js大全
发布时间: 2021年1月13日 | 浏览:
| 分类:js开发
这篇文章主要介绍了微信公众号服务器验证Token步骤图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下服务器验证Token验证分为以下及步骤一,在微信公众号平台上设置1.1打开微信公众号平台1.2打开”开发“中的<基本配置>1.3点击基本配置页面里的修改配置1.4输入URL:url填写:http://外网IP:端口号/wx 。外网IP请到腾讯云购买成功处查询, http的端口号固定使用80,不可填写其他。Token:自主设置,这个token与公众平台wiki中常提的access_token不是一回事。这个token只用于验证开发者服务器。(注:Token可以随便写 写完记住留着备用)EncodingAESKey:点击随机生成现在选择提交肯定是验证token失败,因为还需要完成代码逻辑。改动原先main.py文件,新增handle.py 也可以用wbe文件二,编写后台程序我选用的是web,ashx一般处理程序页面代码源码:
namespace WEF
{
///
/// Token 的摘要说明
///
public class Token : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
ProcesyanzhengsRequest(context);//执行下面方法
}
public bool IsReusable
{
get
{
return false;
}
}
public void ProcesyanzhengsRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string token = " ";//输入你上面自己编写的Token
if (string.IsNullOrEmpty(token))
{
return;
}
//取到Token接收到的值
string echoString = HttpContext.Current.Request.QueryString["echoStr"];
string signature = HttpContext.Current.Request.QueryString["signature"];
string timestamp = HttpContext.Current.Request.QueryString["timestamp"];
string nonce = HttpContext.Current.Request.QueryString["nonce"];
if (CheckSignature(token, signature, timestamp, nonce)) //判断验证是否正确
{
if (!string.IsNullOrEmpty(echoString))
正确返回微信服务器
{
HttpContext.Current.Response.Write(echoString);
HttpContext.Current.Response.End();
}
}
}
///
/// 验证微信签名
///
public 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 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
//tmpStr = Membership.CreateUser(tmpStr, "SHA1");
tmpStr = tmpStr.ToLower();
if (tmpStr == signature) //如果计算后得到的数值与传过来的数值相等
{
return true;
//返回正确
}
else
{
return false; //不相等 返回错误
}
}
}
}
namespace WEF
{
///
/// Token 的摘要说明
///
public class Token : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
ProcesyanzhengsRequest(context);//执行下面方法
}
public bool IsReusable
{
get
{
return false;
}
}
public void ProcesyanzhengsRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string token = " ";//输入你上面自己编写的Token
if (string.IsNullOrEmpty(token))
{
return;
}
//取到Token接收到的值
string echoString = HttpContext.Current.Request.QueryString["echoStr"];
string signature = HttpContext.Current.Request.QueryString["signature"];
string timestamp = HttpContext.Current.Request.QueryString["timestamp"];
string nonce = HttpContext.Current.Request.QueryString["nonce"];
if (CheckSignature(token, signature, timestamp, nonce)) //判断验证是否正确
{
if (!string.IsNullOrEmpty(echoString))
正确返回微信服务器
{
HttpContext.Current.Response.Write(echoString);
HttpContext.Current.Response.End();
}
}
}
///
/// 验证微信签名
///
public 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 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
//tmpStr = Membership.CreateUser(tmpStr, "SHA1");
tmpStr = tmpStr.ToLower();
if (tmpStr == signature) //如果计算后得到的数值与传过来的数值相等
{
return true;
//返回正确
}
else
{
return false; //不相等 返回错误
}
}
}
}三,上传到服务器1.打开D:Web.config
connectionString="server=.;database=WEF;uid=sa;pwd=1@#;Enlist=true;Pooling=true;Max Pool Size=300;Min Pool Size=0;Connection Lifetime=300;user id=sa;packet size=1000"/>
connectionString="server=.;database=WEF;uid=sa;pwd=1@#;Enlist=true;Pooling=true;Max Pool Size=300;Min Pool Size=0;Connection Lifetime=300;user id=sa;packet size=1000"/>
2.将server地址改成。将database文件夹名称改成你写的代码文件夹名称3.右键点击文件 点击发布4.点击发布5.打开文件夹 右键点击将文件解压6.打开服务器7.将解压的文件 粘贴到桌面上 粘贴好解压成文件9.将解压好的文件放到你的服务器C盘自定义的文件夹内三。部署服务器1.点击服务器开始菜单 打开IIS管理器点击IP2.右键点击网站 点击添加网站网站网站名称自己写路径选择你文件粘贴到C盘的文件夹主机名称填写你部署网站的网址点击确定就行了3.点击应用程序池4.将刚添加的网站端口从2.0改成4.0 保存即可再点你添加的网站 击浏览网站 将会出现”服务器错误页面“ 出现这个页面就算已经部署完毕四,打开微信公众号平台 修改配置页面直接点击提交可能会登陆超时 的从新登陆 编写修改配置从新填写一遍 并把URL Token 填写正确 确保跟上次填写的一样点击提交即可以上就是本文的全部内容,希望对大家的学习有所帮助。
namespace WEF
{
///
/// Token 的摘要说明
///
public class Token : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
ProcesyanzhengsRequest(context);//执行下面方法
}
public bool IsReusable
{
get
{
return false;
}
}
public void ProcesyanzhengsRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string token = " ";//输入你上面自己编写的Token
if (string.IsNullOrEmpty(token))
{
return;
}
//取到Token接收到的值
string echoString = HttpContext.Current.Request.QueryString["echoStr"];
string signature = HttpContext.Current.Request.QueryString["signature"];
string timestamp = HttpContext.Current.Request.QueryString["timestamp"];
string nonce = HttpContext.Current.Request.QueryString["nonce"];
if (CheckSignature(token, signature, timestamp, nonce)) //判断验证是否正确
{
if (!string.IsNullOrEmpty(echoString))
正确返回微信服务器
{
HttpContext.Current.Response.Write(echoString);
HttpContext.Current.Response.End();
}
}
}
///
/// 验证微信签名
///
public 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 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
//tmpStr = Membership.CreateUser(tmpStr, "SHA1");
tmpStr = tmpStr.ToLower();
if (tmpStr == signature) //如果计算后得到的数值与传过来的数值相等
{
return true;
//返回正确
}
else
{
return false; //不相等 返回错误
}
}
}
}
namespace WEF
{
///
/// Token 的摘要说明
///
public class Token : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
ProcesyanzhengsRequest(context);//执行下面方法
}
public bool IsReusable
{
get
{
return false;
}
}
public void ProcesyanzhengsRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string token = " ";//输入你上面自己编写的Token
if (string.IsNullOrEmpty(token))
{
return;
}
//取到Token接收到的值
string echoString = HttpContext.Current.Request.QueryString["echoStr"];
string signature = HttpContext.Current.Request.QueryString["signature"];
string timestamp = HttpContext.Current.Request.QueryString["timestamp"];
string nonce = HttpContext.Current.Request.QueryString["nonce"];
if (CheckSignature(token, signature, timestamp, nonce)) //判断验证是否正确
{
if (!string.IsNullOrEmpty(echoString))
正确返回微信服务器
{
HttpContext.Current.Response.Write(echoString);
HttpContext.Current.Response.End();
}
}
}
///
/// 验证微信签名
///
public 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 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
//tmpStr = Membership.CreateUser(tmpStr, "SHA1");
tmpStr = tmpStr.ToLower();
if (tmpStr == signature) //如果计算后得到的数值与传过来的数值相等
{
return true;
//返回正确
}
else
{
return false; //不相等 返回错误
}
}
}
}三,上传到服务器1.打开D:Web.config
connectionString="server=.;database=WEF;uid=sa;pwd=1@#;Enlist=true;Pooling=true;Max Pool Size=300;Min Pool Size=0;Connection Lifetime=300;user id=sa;packet size=1000"/>
connectionString="server=.;database=WEF;uid=sa;pwd=1@#;Enlist=true;Pooling=true;Max Pool Size=300;Min Pool Size=0;Connection Lifetime=300;user id=sa;packet size=1000"/>