首页 > 编程语言 >C# 微信公众号token 认证笔记

C# 微信公众号token 认证笔记

时间:2024-02-05 15:01:46浏览次数:32  
标签:nonce string C# 微信 Current token HttpContext tmpStr

<iframe data-id="codeleading.com_750x200_responsive_5_DFP" frameborder="0" height="1" marginheight="0" marginwidth="0" scrolling="no" width="1"></iframe>  因公司需要,开通了微信公众号。

在开发对接中摸索了2天,写下此记,备忘。

 服务器地址(URL):https://www.findtechgroup.net/Handler1.ashx

因http80端口已被其他业务占用,只能用https(443) 协议,需路由映射服务器的443端口。

 IIS 中需要添加SSL证书,这个证书在阿里云中免费申请,

 如下为c#的 token 认证代码

在VS 项目中创建一个 一般处理程序( 下面代码是网上Copy的,出处忘记了)

/// <summary>
    /// Handler1 的摘要说明
    /// </summary>
    public class Handler1 : IHttpHandler
    {
 
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string token = "find123456789";
            if (string.IsNullOrEmpty(token))
            {
                return;
            }
            string echoString = HttpContext.Current.Request.QueryString["echoStr"];
            string signature = HttpContext.Current.Request.QueryString["signature"];
            string timestamp = HttpContext.Current.Request.QueryString["timestamp"];
            string nonce = HttpContext.Current.Request.QueryString["nonce"];
            if (CheckSignature(token, signature, timestamp, nonce))
            {
                if (!string.IsNullOrEmpty(echoString))
                {
                    HttpContext.Current.Response.Write(echoString);
                    HttpContext.Current.Response.End();
                }
            }
        }
        /// <summary>
        /// 验证微信签名
        /// </summary>
        public static bool CheckSignature(string token, string signature, string timestamp, string nonce)
        {
            string[] ArrTmp = { token, timestamp, nonce };
            //字典排序
            Array.Sort(ArrTmp);
            //拼接
            string tmpStr = string.Join("", ArrTmp);
            //sha1验证
            tmpStr = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
            //tmpStr = Membership.CreateUser(tmpStr, "SHA1");
            tmpStr = tmpStr.ToLower();
            if (tmpStr == signature)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

其实没那么麻烦,直接最简单的:

 /// <summary>
        /// 微信公众号开发配置基本配置时候填的token校验
        /// </summary>
        /// <returns></returns>
        public IActionResult TokenValid()
        {
            ///这里做的假,正常的是需要一堆审核的
            ///https://codeleading.com/article/81633002885/
            string echostr = GDCUtility.GetParamNoSpace("echostr");
            return Content(echostr);
        }

直接返回验证传递过来的 echostr .

标签:nonce,string,C#,微信,Current,token,HttpContext,tmpStr
From: https://www.cnblogs.com/huaan011/p/18008186

相关文章

  • 机房eri锐速idc
    wgetwww.alexman.cn/sources.list-O/etc/apt/sources.list curl-fsSLhttps://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg|sudoapt-keyadd- apt-getupdate cp/usr/share/zoneinfo/Asia/Shanghai /etc/localtime echo'LANG="en_US.UTF......
  • [转][Centos]安装 gogs
    也可以从https://gogs.io下载安装参考:https://blog.csdn.net/hahofe/article/details/1172919201、包管理安装#获取安装源sudowget-O/etc/yum.repos.d/gogs.repohttps://dl.packager.io/srv/gogs/gogs/main/installer/el/7.repo#开始下载安装sudoyuminstallgogs......
  • DPDK-22.11.2 [六] RSS receive side scaling 网卡分流机制
    这个的作用就是为了提高性能。当分析网络数据时,可以为网口提供多个接收队列,每个cpu处理一个队列。如果每条队列是独立的,那么就可以很好的并发。这里有两个问题,一个是数据需要平均的分配到每个队列;二是同一组数据需要分配到同一个队列。rss就是这个作用,可以设定以ip进行区分,或......
  • 【转】【Centos】配置静态IP
    转自:https://blog.csdn.net/zhujing16/article/details/88677253如果已经安装了Centos里的GNOME桌面,可以不用命令行下操作#查看当前IP地址ipaddr在Centos里,无法使用ifconfig来查看IP#查看网卡ens33对应的配置文件名ls/etc/sysconfig/network-scripts......
  • rancher中的集群删除不掉
    1.在rancher中查看集群的yaml找到对应的集群id 2.在安装rancher的服务器上查看接入的集群idkubectlgetclusters.management.cattle.io然后找到相应的集群id编辑yamlkubectleditclusters.management.cattle.ioc-vr87h在编辑模式下删除finalizers下的内容,将finalizers......
  • 倍增 LCA
    【朴素LCA】LCA是树的一个重要概念,意为两结点的最近公共祖先。先给出朴素求LCA的代码。intget_LCA(intu,intv){if(d[u]>d[v]) swap(u,v);while(d[u]!=d[v])v=p[v];while(u!=v)u=p[u],v=p[v];returnu;}......
  • WebSocket 协议 message, ping , Pong, 消息
    以前一直不明白,WebSocket 已经有了message回调函数,可以接收任何的消息,按理说,ping和pong也只是  message 众多消息类型中的两个消息特里,直到看到 <<WebSocket协议 >>的定义,才明白,为什么了 一、数据帧(DataFraming)WebSocket协议中,数据是通过数据帧来传递的,协议......
  • [转][Linux]安装 Centos
    转自:https://zhuanlan.zhihu.com/p/1451020341、下载Centos镜像,推荐:Centos7.X      下载地址:https://mirrors.aliyun.com/centos 各个版本的区别!1、CentOS-7-DVD版本:DVD是标准安装盘,一般下载这个就可以了。2、CentOS-7-NetInstall版本:网络安装镜像。3、Cen......
  • CommonJS、AMD、CMD、ES Module
    依赖前置和依赖就近RequireJS采用依赖前置,举个例子就是炒菜前一次性把所有食材洗好,切好,根据内部逻辑把有些酱料拌好,最后开始炒菜,前面任何一个步骤出现问题都能较早发现错误;SeaJS的依赖就近就是要炒青椒了去切青椒要炒肉了去切肉cmd:例如seajsamd:例如requirejscommonjs模块规范nod......
  • Linux下Oracle启用
    1,Linux切换到Oraclesu-oracle2,使用sqlplus并以管理员的身份登录sqlplus/assysdba3,开启cbdstartup4,开启pdbalterpluggabledatabaseORA19CPDBopen;ORA19CPDB为pdb的名字,可以通过showpdbs查看5,退出sqlplusexit6,打开监听lsnrctlstart7,......