• 微信公众号关注/取消关注回调.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>";
    }
    }

  • 相关阅读:
    AtomicReference与volatile的区别
    深度剖析ConcurrentHashMap(转)
    ConcurrentHashMap原理分析
    Java Stack源码分析
    Fail-Fast机制详解
    TreeSet源码分析
    状态(State)模式
    原型(Prototype)模式
    职责连模式
    观察者模式(Observer)
  • 原文地址:https://www.cnblogs.com/wang150601/p/16291353.html
Copyright © 2020-2023  润新知