微信公众号禁止设置ngrok地址的解决办法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微信公众号禁止设置ngrok地址的解决办法相关的知识,希望对你有一定的参考价值。

最近想改一下以前测试公众号的地址为ngrok2的亚洲节点,结果提示安全问题,禁止设置。原来是微信把类似花生壳ngrok等这些地址都封了。

现在暂时用的解决办法是在外网服务器上做一个转发程序,只需一个handler

using System.Configuration;
using System.Diagnostics;
using System.Net;
using System.Web;

namespace WechatProxy
{
    public class WechatHandler : IHttpHandler
    {
        private TraceSource trace;
        public WechatHandler()
        {
            IsReusable = true;
            trace = new TraceSource("trace");
        }
        public void ProcessRequest(HttpContext context)
        {

            var url = string.Format("http://{0}{1}{2}", ConfigurationManager.AppSettings["host"],
                context.Request.Path,
                context.Request.QueryString.Count > 0 ? ("?" + context.Request.QueryString) : "");
            trace.TraceInformation(url);
            var request = WebRequest.Create(url);
            if (context.Request.RequestType == "POST")
            {
                request.Method = "POST";
                using (var reqstream = request.GetRequestStream())
                {
                    context.Request.InputStream.CopyTo(reqstream);
                }
            }
            var response = request.GetResponse();
            using (var resstream = response.GetResponseStream())
            {
                resstream.CopyTo(context.Response.OutputStream);
            }
        }

        public bool IsReusable { get; private set; }
    }
}
<?xml version="1.0" encoding="utf-8"?>

<configuration>
  <appSettings>
    <add key="host" value="xxxx.ap.ngrok.io" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <httpHandlers>
      <clear />
      <add type="WechatProxy.WechatHandler" path="*" verb="GET,POST" />
    </httpHandlers>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
  <system.diagnostics>
    <sources>
      <source name="trace" switchName="SourceSwitch" switchType="System.Diagnostics.SourceSwitch">
        <listeners>
          <clear/>
          <add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="WechatProxy" />
        </listeners>
      </source>
    </sources>
    <switches>
      <add name="SourceSwitch" value="Verbose"/>
      <!--add name="SourceSwitch" value="Off Verbose Information Warning Error Critical All" -->
    </switches>
    <trace autoflush="true" indentsize="4"/>
  </system.diagnostics>
</configuration>

 

以上是关于微信公众号禁止设置ngrok地址的解决办法的主要内容,如果未能解决你的问题,请参考以下文章

flask+ngrok微信公众号搭建小试

微信公众号开发80端口映射之搭建ngrok服务器

java微信公众号开发----搭建ngrok环境

微信公众号开发80端口映射解决方案

微信公众号开发

微信小程序提示ip禁止访问