微信授权(Net Mvc)

项目结构

微信授权(Net Mvc)

WeiXinController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Text;
using System.Xml;
using System.Net;
using Newtonsoft.Json;

using WeChat2.Models;

namespace WeChat2.Controllers
{
    using Senparc.Weixin.MP;
    public class WeiXinController : Controller
    {
        string token = "garfieldzf8";

        string appID = "wx3475193134aa161e";
        string appsecret = "10c0994def4d52442a2edde4ce1843cf";


        [HttpGet]
        [ActionName("Index")]
        public ActionResult Get(string signature, string timestamp, string nonce, string echostr)
        {
            if (CheckSignature.Check(signature, timestamp, nonce, token))
            {
                return Content(echostr);
            }
            else
            {
                return Content("err");
            }

        }

        [HttpPost]
        [ActionName("Index")]
        public ActionResult Get(string signature, string timestamp, string nonce)
        {
            StreamReader sr = new StreamReader(Request.InputStream, Encoding.UTF8);
            XmlDocument doc = new XmlDocument();
            doc.Load(sr);
            sr.Close();
            sr.Dispose();

            WxMessage wxMessage = new WxMessage();
            wxMessage.ToUserName = doc.SelectSingleNode("xml").SelectSingleNode("ToUserName").InnerText;
            wxMessage.FromUserName = doc.SelectSingleNode("xml").SelectSingleNode("FromUserName").InnerText;
            wxMessage.MsgType = doc.SelectSingleNode("xml").SelectSingleNode("MsgType").InnerText;
            wxMessage.CreateTime = int.Parse(doc.SelectSingleNode("xml").SelectSingleNode("CreateTime").InnerText);

            Log(wxMessage.ToUserName + "," + wxMessage.FromUserName + "," + wxMessage.MsgType);

            if (wxMessage.MsgType == "event")
            {
                wxMessage
上一篇:10分钟教你用Python玩转微信之抓取好友个性签名制作词云


下一篇:关于微信企业付款到零钱X509Certificate2读取证书信息,发布到服务器访问不到的解决方案