标签:服务器 文件 sha 数据 数字 public 写入 shp 获取
看了网上,基本都是php,java的,我这个亲自试验过,好用
接口地址:https://www.sywebsoft.com
验证文件:weixin.aspx
你们把这个保存成aspx文件,并放到对应的地址里,并在微信里做配置就行了
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.Web.Security;
using System.Xml;
public partial class weixin : System.Web.UI.Page
{
const string Token = "myweixin";
protected void Page_Load(object sender, EventArgs e)
{
if (Request.HttpMethod.ToLower() == "post")
{
ResponseMsg(GetPostMsg());
}
else
{
Valid();
}
}
private string GetPostMsg()
{
try
{
System.IO.Stream s = Request.InputStream;
int count = 0;
byte[] buffer = new byte[s.Length];
StringBuilder builder = new StringBuilder();
while ((count = s.Read(buffer, 0, buffer.Length)) > 0)
{
builder.Append(Request.ContentEncoding.GetString(buffer, 0, count));
}
s.Flush();
s.Close();
s.Dispose();
return builder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
private int ConvertDateTimeInt(System.DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
return (int)(time - startTime).TotalSeconds;
}
private void ResponseMsg(string _postStr)
{
//try
// {
XmlDocument document = new XmlDocument();
document.LoadXml(_postStr);
XmlNode root = document.SelectSingleNode("xml");
XmlNodeList nodeList = root.ChildNodes;
//model存储获取的数据
var model = new
{
ToUserName = nodeList.Item(0).InnerText,
FromUserName = nodeList.Item(1).InnerText,
CreateTime = nodeList.Item(2).InnerText,
MsgType = nodeList.Item(3).InnerText,
Content = nodeList.Item(4).InnerText
};
//获取当前的时间
int currentTime = ConvertDateTimeInt(DateTime.Now);
string xml = "<xml><ToUserName><![CDATA[" + model.ToUserName + "]]></ToUserName><FromUserName><![CDATA[" + model.FromUserName + "]]></FromUserName><CreateTime>" + DateTime.Now + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[fgdsfgsdf]]></Content></xml>";
Response.Write(xml);
//switch (model.Content.Substring(0, 2))
//{
// case "你好": Response.Write("nihao");
// break;
// case "图片": Response.Write("tupian");
// break;
// case "电影": Response.Write("dianying");
// break;
// default:
// Response.Write("dddd");
// break;
//}
//}
//catch (Exception ex)
//{
// //错误信息,写入日志
// //WriteTextLog(ex.Message);
//}
}
//验证token
private void Valid()
{
string echoStr = Request.QueryString["echoStr"].ToString();//微信自动传过来的echostr
if (CheckSignature())
{
if (!string.IsNullOrEmpty(echoStr))//当随机数和token验证成功后,返回echostr给微信,就可以了
{
Response.Write(echoStr);
Response.End();
}
}
}
/// <summary>
/// 验证微信签名
/// </summary>
/// * 将token、timestamp、nonce三个参数进行字典序排序
/// * 将三个参数字符串拼接成一个字符串进行sha1加密
/// * 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。
/// <returns></returns>
private bool CheckSignature()
{
string signature = Request.QueryString["signature"].ToString();
string timestamp = Request.QueryString["timestamp"].ToString();
string nonce = Request.QueryString["nonce"].ToString();
string[] ArrTmp = { Token, timestamp, nonce };
Array.Sort(ArrTmp);//字典排序
string tmpStr = string.Join("", ArrTmp);
tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");//对该字符串进行sha1加密
tmpStr = tmpStr.ToLower();//对字符串中的字母部分进行小写转换,非字母字符不作处理
if (tmpStr == signature)//开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。开发者通过检验signature对请求进行校验,若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,否则接入失败
{
return true;
}
else
return false;
}
}
标签:服务器 文件 sha 数据 数字 public 写入 shp 获取
原文地址:https://www.cnblogs.com/lengyuye2019/p/12073816.html