• 微信公众号关注/取消关注回调.net core 5.0 ^


    [HttpPost, HttpGet]
    public async Task WXSubscribeCallBack()
    {
    WeiXinXmlmessage wx = new WeiXinXmlmessage();
    //校验微信公众号服务器配置是否一致
    if (HttpContext.Request.Method.ToLower() == "get")
    {
    string signature = HttpContext.Request.Query["signature"];
    string timestamp = HttpContext.Request.Query["timestamp"];
    string nonce = HttpContext.Request.Query["nonce"];
    string echostr = HttpContext.Request.Query["echostr"];
    string token = "hbzsb1234567";
    List<string> list = new List<string>() { token, timestamp, nonce };
    list.Sort();
    string data = string.Join("", list);
    byte[] temp1 = Encoding.UTF8.GetBytes(data);
    SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();
    byte[] temp2 = sha.ComputeHash(temp1);
    var hashCode = BitConverter.ToString(temp2);
    hashCode = hashCode.Replace("-", "").ToLower();
    if (hashCode == signature)
    {
    Response.WriteAsync(echostr);
    }
    }
    else
    {
    HttpContext.Request.EnableBuffering();
    Stream TmpBody = HttpContext.Request.Body;
    string requestData = "";
    TmpBody.Position = 0;
    using (StreamReader streamReader = new StreamReader(TmpBody, Encoding.UTF8))
    {
    //读取Body的时候,请尽量使用异步方式读取。ASP.NET Core默认是不支持同步读取的,会抛出异常
    //解决方法 启用 KestrelServerOptions 中 AllowSynchronousIO
    requestData = streamReader.ReadToEndAsync().Result;
    TmpBody.Position = 0;
    }
    XmlDocument xml = new XmlDocument();
    xml.LoadXml(requestData);

    wx.ToUserName = xml.SelectSingleNode("xml").SelectSingleNode("ToUserName").InnerText;
    wx.FromUserName = xml.SelectSingleNode("xml").SelectSingleNode("FromUserName").InnerText;
    wx.MsgType = xml.SelectSingleNode("xml").SelectSingleNode("MsgType").InnerText;

    if (wx.MsgType.Trim() == "event")
    {
    //是否关注
    wx.EventName = xml.SelectSingleNode("xml").SelectSingleNode("Event").InnerText;
    if (wx.EventName.Trim() == "subscribe" || wx.EventName.Trim() == "unsubscribe")
    {
    //处理业务逻辑
    var openid = wx.FromUserName;
    }
    //测试返回关注回复
    if (wx.EventName.Trim() == "subscribe")
    {
    var messageXml = sendTextMessage(wx, "关注成功!");
    HttpContext.Response.WriteAsync(messageXml);
    }
    }
    }
    }

    /// <summary>
    /// 发送文字消息
    /// </summary>
    /// <param name="wx" />获取的收发者信息
    /// <param name="content" />内容
    /// <returns></returns>
    private string sendTextMessage(WeiXinXmlmessage wx, string content)
    {
    string res = string.Format(Message_Text,
    wx.FromUserName, wx.ToUserName, DateTime.Now.Ticks, content);
    return res;
    }
    /// <summary>
    /// 普通文本消息
    /// </summary>
    private static string Message_Text
    {
    get
    {
    return @"<xml>
    <ToUserName><![CDATA[{0}]]></ToUserName>
    <FromUserName><![CDATA[{1}]]></FromUserName>
    <CreateTime>{2}</CreateTime>
    <MsgType><![CDATA[text]]></MsgType>
    <Content><![CDATA[{3}]]></Content>
    </xml>";
    }
    }

  • 相关阅读:
    公号文章模板
    css 网格线
    刷题笔记-图-图的存储
    PAT Advanced A1021 Deepest Root (25) [图的遍历,DFS,计算连通分量的个数,BFS,并查集]
    PAT Advanced 1013 Battle Over Cities (25) [图的遍历,统计连通分量的个数,DFS,BFS,并查集]
    PAT Advanced 1076 Forwards on Weibo (30) [图的遍历,BFS,DFS]
    PAT Advanced 1034 Head of a Gang (30) [图的遍历,BFS,DFS,并查集]
    堆排序
    PAT Advanced 1155 Heap Paths (30) [DFS, 深搜回溯,堆]
    PAT Advanced 1098 Insertion or Heap Sort (25) [heap sort(堆排序)]
  • 原文地址:https://www.cnblogs.com/wang150601/p/16291353.html
Copyright © 2020-2023  润新知