/// <summary>
        /// 获取公众号的AccessToken
        /// </summary>
        /// <returns>微信提供的AccessToken</returns>
        public string GetAccessToken()
        {
            if (WeChatConfig == null)
            {
                throw new NullReferenceException("请使用构造ConnectLinkUp(idOrAppId),或初始化Initialize(idOrAppId)");
            }
            string accessToken = String.Empty;
            //先从数据库获取Access_Token,如果不存在或已过期,则重新跟微信拿Access_Token
            string appId          = WeChatConfig.AppID;
            string accessTokenKey = string.Format(RedisKeyPrefix.WECHAT_ACCESS_TOKEN, appId);
            string updateTokenKey = string.Format(RedisKeyPrefix.WECHAT_TOKEN_CONCURRENT, appId);

            do
            {
                if ("Concurrent".Equals(accessToken))
                {
                    SystemLogHelper.Info(MethodBase.GetCurrentMethod(), "GetAccessToken(),已有线程直接去微信获取AccessToken,在此等待");
                    Thread.Sleep(500);//发生并发的线程需要回到这里,等待单线程更新完成
                }
                AccessToken wechatAccessToken = redisHelper.StringGet <AccessToken>(accessTokenKey);
                if (wechatAccessToken != null)
                {
                    accessToken = wechatAccessToken.access_token;
                }
                else//Redis里面的Token已经失效,需要单线程去更新Token
                {
                    accessToken = ConcurrentControl.SingleUserFunc(updateTokenKey, "Concurrent", () =>
                    {
                        SystemLogHelper.Info(MethodBase.GetCurrentMethod(), "获取公众号的AccessToken,GetAccessToken(),直接去微信获取AccessToken");
                        return(GetWeChatAccessToken());
                    });
                }
            } while ("Concurrent".Equals(accessToken));
            return(accessToken);
        }
Пример #2
0
        /// <summary>
        /// 正式写入日志到Redis
        /// </summary>
        /// <typeparam name="TState">泛型</typeparam>
        /// <param name="logLevel">日志等级</param>
        /// <param name="eventId">日志触发事件</param>
        /// <param name="state">泛型实体</param>
        /// <param name="exception">异常信息</param>
        /// <param name="formatter">格式化处理</param>
        public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            if (!IsEnabled(logLevel))
            {
                return;
            }
            var msg = $"日志触发ID:{eventId.Id},日志名:{eventId.Name},具体》》" + formatter(state, exception);

            switch (logLevel)
            {
            case LogLevel.Trace:
            case LogLevel.Debug:
                SystemLogHelper.Debug(moduleName, msg, exception);
                break;

            case LogLevel.Information:
                SystemLogHelper.Info(moduleName, msg, exception);
                break;

            case LogLevel.Warning:
                SystemLogHelper.Warn(moduleName, msg, exception);
                break;

            case LogLevel.Error:
                SystemLogHelper.Error(moduleName, msg, exception);
                break;

            case LogLevel.Critical:
                SystemLogHelper.Fatal(moduleName, msg, exception);
                break;

            default: break;
            }
        }
Пример #3
0
        /// <summary>
        /// 对微信推送过来的消息进行解密
        /// </summary>
        /// <param name="request">推送过来的请求</param>
        /// <returns>解密后的xml明文</returns>
        private string MessageDecrypt(WeChatEncryptMsg requestEncryptMsg)
        {
            //string signature = request.QueryString["signature"];
            //string timestamp = request.QueryString["timestamp"];
            //string nonce = request.QueryString["nonce"];
            //string openid = request.QueryString["openid"];
            //string encrypt_type = request.QueryString["encrypt_type"];//aes
            //string msg_signature = request.QueryString["msg_signature"];
            var          stream     = requestEncryptMsg.Body; //具体消息数据在请求流里面
            StreamReader reader     = new StreamReader(stream);
            string       ciphertext = reader.ReadToEnd();     //密文消息

            reader.Close();
            string xmlMsgTxt = null;  //解析之后的明文

            if (ciphertext.Contains("<Encrypt>") && ciphertext.Contains("</Encrypt>"))
            {
                int result = wxcpt.DecryptMsg(requestEncryptMsg.MsgSignature,
                                              requestEncryptMsg.Timestamp, requestEncryptMsg.Nonce, ciphertext, ref xmlMsgTxt);
                SystemLogHelper.Info(GetType().FullName, "消息解密处理结果:" + CryptResult(result));
                weChatConfig.EnCrypt = true;//确实启用了加密方式
            }
            else
            {
                xmlMsgTxt            = ciphertext;
                weChatConfig.EnCrypt = false;//并没有启用加密方式
            }
            return(xmlMsgTxt);
        }
Пример #4
0
 /// <summary>
 /// 输出Info的日志提示
 /// </summary>
 /// <param name="message">日志提示消息</param>
 /// <param name="exception">可指定发生的异常对象</param>
 /// <param name="isWriteLog">是否需要记录到系统日志,默认需要</param>
 public void Info(string message, Exception exception = null, bool isWriteLog = true)
 {
     showLog(message + exception?.GetExceptionMsg()
             + $"---{DateTime.Now} ThreadId:{Thread.CurrentThread.ManagedThreadId}\r\n", Color.Green);
     if (isWriteLog)
     {
         SystemLogHelper.Info(MethodBase.GetCurrentMethod(), message, exception);
     }
 }
Пример #5
0
        /// <summary>
        /// 以推送过来请求中的随机字符串、时间戳为基础进行响应消息加密
        /// </summary>
        /// <param name="xmlMsgTxt">要加密的明文</param>
        /// <returns>加密后的密文</returns>
        private string MessageEncrypt(string xmlMsgTxt)
        {
            string timestamp   = TimeHelper.GetTime(DateTime.Now).ToString();
            string nonce       = StringHelper.RandomStr(StrType.NumAndLowercase, 16);
            string sEncryptMsg = null; //xml格式的密文
            int    result      = wxcpt.EncryptMsg(xmlMsgTxt, timestamp, nonce, ref sEncryptMsg);

            SystemLogHelper.Info(GetType().FullName, "消息加密处理结果:" + CryptResult(result));
            return(sEncryptMsg);
        }
Пример #6
0
        public IActionResult Index(string id)
        {
            string signature = Request.Query["signature"];
            string timestamp = Request.Query["timestamp"];
            string nonce     = Request.Query["nonce"];

            SystemLogHelper.Info(GetType().FullName, $"微信公众号接入参数:signature={signature},timestamp={timestamp},nonce={nonce}");
            //RedisHelper redisHelper = new RedisHelper();
            //string readyAppId = redisHelper.StringGet(RedisKeyPrefix.WECHAT_CONNECT_READY);
            connectLinkUp.Initialize(id);//确定公众号
            if (connectLinkUp.CheckSignature(signature, timestamp, nonce))
            {
                SystemLogHelper.Info(GetType().FullName, $"匹配微信公众号AppId:{id}");
                return(Content(Request.Query["echostr"]));
            }
            return(Json(new ResultEntity <object>(ResultCode.SUCCESS, new { signature, timestamp, nonce })));
        }