首页 > 代码库 > MVC下c#对接微信公众平台开发者模式

MVC下c#对接微信公众平台开发者模式

技术分享

 

在ashx文件中进行HttpContext的处理:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Biz.WX;

namespace weixinplat
{
    /// <summary>
    /// WeiXin 的摘要说明
    /// </summary>
    public class WeiXin : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                string postString = string.Empty;//初始化空字符串来转抓到的request请求的bit流
                context.Response.ContentType = "text/plain";
                if (context.Request.HttpMethod.ToLower() == "post")//如果从requset的httpmethod方法是post就进行图文等处理
                {

                }
                else//否则就是get方式,进行校验认证
                {
                    AccessToken.Auth();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            
            
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

在专门处理数据的类库Biz中校验:

其中AccessToken类用来处理token:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Configuration;

namespace Biz.WeiXin
{
    public class AccessToken
    {
        /// <summary>
        /// 这个函数是初始化配置服务器地址
        /// </summary>
        public static void Auth()
        {
            string echoStr = HttpContext.Current.Request.QueryString["echoStr"];
            if (CheckSignature()) //校验签名是否正确
            {
                if (!string.IsNullOrEmpty(echoStr))
                {
                    HttpContext.Current.Response.Write(echoStr);
                    HttpContext.Current.Response.End();

                }
            }
        }
        /// <summary>
        /// 校验微信公众平台签名函数
        /// </summary>
        /// <returns></returns>
        public static bool CheckSignature()
        {
            string signature = HttpContext.Current.Request.QueryString["signature"];
            string timestamp = HttpContext.Current.Request.QueryString["timestamp"];
            string nonce = HttpContext.Current.Request.QueryString["nonce"];
            string token = ConfigurationManager.AppSettings["weixin_token"];

            string[] tmpArr = { token , timestamp, nonce };
            Array.Sort(tmpArr);
            string tmpStr = string.Join("", tmpArr);
            tmpStr = WX.Sha1_Hash(tmpStr);

            if (tmpStr == signature)
            {
               
                return true;
            }
            else
            {
                return
                    false;
            }


        }
    }
}

WX类用来处理哈希算法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Biz.WX
{
    public class WX
    {
        public static string Sha1_Hash(string str_sha1_in)
        {
            SHA1 sha1 = new SHA1CryptoServiceProvider();
            byte[] bytes_sha1_in = UTF8Encoding.Default.GetBytes(str_sha1_in);
            byte[] bytes_sha1_out = sha1.ComputeHash(bytes_sha1_in);
            string str_sha1_out = BitConverter.ToString(bytes_sha1_out);
            str_sha1_out = str_sha1_out.Replace("-", "").ToLower();
            return str_sha1_out;
        }
    }
}

 

MVC下c#对接微信公众平台开发者模式