一.填写服务器配置
登录微信公众平台,单击开发者中心,然后单击“更改配置”按钮,填写服务器地址(URL )、Token和EncodingAESKey。 URL是开发人员用于接收微信消息和事件的接口URL。 Token可以任意填写,用于生成签名。 EncodingAESKey可以手动填写,也可以随机生成。
二.验证服务器的有效性
以下内容来自开发者的文档
开发者发送信息后,微信服务器会向填写的服务器地址URL发送获取请求。 GET请求具有以下四个参数:
参数说明
信号
微信加密签名,signature是开发者填写的token参数与请求中的timestamp参数、nonce参数的组合。
时间舞台
时间戳
诺克斯
随机数
echostr
随机字符串
开发者通过验证signature来验证请求(验证方式如下)。 如果确认这次的获取请求来自微信服务器,请直接返回echostr参数的内容。 将启用访问,并作为开发人员成功。 否则,访问将失败。
加密/验证过程如下。
按词典顺序对token、timestamp、nonce三个参数进行排序
2 .将3个参数字符串连接成1个字符串进行sha1加密
3 .开发人员获得加密字符串后,可以将其与signature进行比较,以识别该请求来源于微信
实现方法新的一般处理程序WXService.ashx
WXService.ashx.cs
公共处理请求(httpcontext context ) )。
{string postString=string.Empty; httpcontext.current.request.http method.toupper (=' post ' ) ) (if ) ) ) ) )。
{}else{
Auth (;
}
}
//
//签名验证//
私有语音(
{ string token=configuration manager.appsettings [ ' wechattoken ' ].tostring (; //这里是自己填写的tokenif (! string.isnullorempty () Token ) )
{ stringechostring=httpcontext.current.request.query string [ ' echostr ' ]; /*接收微信服务器发送GET请求携带的参数内容*/string signature=httpcontext.current.request.query string [ ' signature ' ] string timestamp=httpcontext.current.request.query string [ ' timestamp ' ]; string nonce=httpcontext.current.request.query string [ ' nonce ' ]; if(newbasicapi ().checksignature(Token,signature,timestamp,nonce ) )
{if (! string.isnullorempty (echostring ) )
{
HTP context.current.response.write (echostring ); 返回echoString
TTP context.current.response.end (;
}
}
}
}
BasicApi.cs
公共检查(string token,string signature,string timestamp,stringnonce ) )。
{string[] ArrTmp={ token,timestamp,nonce };
array.sort(arrtmp; string tmpStr=string.Join (',ArrTmp ),用于对token、timestamp和nonce这三个参数进行排序; 使用String的join方法将数组元素连接成字符串
tmpstr=formsauthentication.hashpasswordforstoringinconfigfile (tmpstr,' SHA1 ' ); //SHA1加密
tmpStr=tmpStr.ToLower (; if(tmpstr==signature )//加密字符串与signature的对比
{返回真;
}else{return false;
}
}
原文: 3358 www.cn blogs.com/wings Wang/p/4595087.html