微信公众平台填写服务器配置提示“token验证失败”

zxdii 2015-03-13 03:50:52
配置URL:http://域名/wxapi.ashx
配置Token:123456
在mvc项目中添加一般处理程序代码如下:

public class wxapi : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("欢迎关注!");
string postString = string.Empty;
if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST")
{
using (Stream stream = HttpContext.Current.Request.InputStream)
{
Byte[] postBytes = new Byte[stream.Length];
stream.Read(postBytes, 0, (Int32)stream.Length);
postString = Encoding.UTF8.GetString(postBytes);
}

if (!string.IsNullOrEmpty(postString))
{
//Execute(postString);
}
}
else
{
Auth(); //微信接入的测试
}
}

public bool IsReusable
{
get
{
return false;
}
}

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

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();
}
}
}


/// <summary>
/// 验证微信签名
/// </summary>
public bool CheckSignature(string token, string signature, string timestamp, string nonce)
{
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;
}
}
}
...全文
870 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
李秀才 2016-10-25
  • 打赏
  • 举报
回复
推荐看下我的博客专栏java微信公众平台开发:http://blog.csdn.net/column/details/13293.html 希望可以帮助到你 。
idragonet 2015-11-19
  • 打赏
  • 举报
回复
要这样写:
        if (CheckSignature(token, signature, timestamp, nonce))
        {
            context.Response.Write(echoString);
        }
        else
        {
            context.Response.Write("验证失败:" + signature);
        }
yusheng622 2015-03-19
  • 打赏
  • 举报
回复
不错!应该可以的
zxdii 2015-03-13
  • 打赏
  • 举报
回复
引用 2 楼 u013685845 的回复:
可以的啊////
可是偏偏验证失败。。。。
伊顺鸣 2015-03-13
  • 打赏
  • 举报
回复
可以的啊////
zxdii 2015-03-13
  • 打赏
  • 举报
回复
补充,用的是80端口

3,143

社区成员

发帖
与我相关
我的任务
社区描述
微信开发即微信公众平台开发,将企业信息、服务、活动等内容通过微信网页的方式进行表现,通过二次开发可以将公众账号由一个媒体型营销工具转化成提供服务的产品。
社区管理员
  • 微信开发
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧