private AuthorizerAccessTokenWrapper CreateWrapper(string appId)
        {
            string refreshToken = _thirdPartyManager.GetAuthorizerRefreshToken(appId);

            if (String.IsNullOrEmpty(refreshToken))
            {
                _log.Write(appId + " 的 RefreshToken 不存在", TraceEventType.Warning);
                return(null);
            }

            RequestApiResult <WeixinThirdPartyGetAuthorizerAccessTokenResult> result =
                ThirdPartyApiWrapper.GetAuthorizerAccessToken(appId, refreshToken);

            if (result.Success)
            {
                AuthorizerAccessTokenWrapper wrapper = new AuthorizerAccessTokenWrapper();
                wrapper.AppId                 = appId;
                wrapper.RefreshToken          = result.ApiResult.RefreshToken;
                wrapper.AccessToken           = result.ApiResult.AccessToken;
                wrapper.AccessTokenExpiryTime = DateTime.Now.AddSeconds(result.ApiResult.ExpiresIn);
                return(wrapper);
            }

            return(null);
        }
示例#2
0
        public static string Handle(string message)
        {
            XElement xml = XElement.Parse(message);

            string messageType = xml.XPathSelectElement("MsgType").Value;

            if (String.IsNullOrEmpty(messageType))
            {
                return(String.Empty);
            }

            _fromUserName = xml.XPathSelectElement("FromUserName").Value;

            //事件消息
            if (messageType == "event")
            {
                string eventType = xml.XPathSelectElement("Event").Value;
                if (String.IsNullOrEmpty(eventType))
                {
                    return(String.Empty);
                }

                ResponsiveXMLMessage_TextMessage textMessage = new ResponsiveXMLMessage_TextMessage();
                textMessage.Content      = eventType + "from_callback";
                textMessage.ToUserName   = _fromUserName;
                textMessage.FromUserName = UserName;
                textMessage.CreateTime   = WeixinApiHelper.ConvertDateTimeToInt(DateTime.Now);

                return(XMLMessageHelper.XmlSerialize(textMessage));
            }
            //普通消息
            else
            {
                string content = xml.XPathSelectElement("Content").Value;
                if (content == "TESTCOMPONENT_MSG_TYPE_TEXT")
                {
                    ResponsiveXMLMessage_TextMessage textMessage = new ResponsiveXMLMessage_TextMessage();
                    textMessage.Content      = "TESTCOMPONENT_MSG_TYPE_TEXT_callback";
                    textMessage.ToUserName   = _fromUserName;
                    textMessage.FromUserName = UserName;
                    textMessage.CreateTime   = WeixinApiHelper.ConvertDateTimeToInt(DateTime.Now);

                    return(XMLMessageHelper.XmlSerialize(textMessage));
                }
                else
                {
                    _query_auth_code = content.Remove(0, "QUERY_AUTH_CODE:".Length);

                    RequestApiResult <WeixinThirdPartyGetAuthorizationInfoResult> getAuthorizationInfo =
                        ThirdPartyApiWrapper.GetAuthorizationInfo(_query_auth_code);

                    _accessToken = getAuthorizationInfo.ApiResult.AuthorizationInfo.AccessToken;

                    _timer = new System.Threading.Timer(SendKFMessage, null, 2 * 1000, 1 * 60 * 1000);

                    return(null);
                }
            }
        }
        public ActionResult AuthorizationCallbackDemo(string auth_code)
        {
            RequestApiResult <WeixinThirdPartyGetAuthorizationInfoResult> result =
                ThirdPartyApiWrapper.GetAuthorizationInfo(auth_code);

            AuthorizationCallbackDemoViewModel model = new AuthorizationCallbackDemoViewModel();

            model.Info = result.ApiResult.AuthorizationInfo;
            return(View(model));
        }
        private void UpdateWrapper(AuthorizerAccessTokenWrapper wrapper)
        {
            RequestApiResult <WeixinThirdPartyGetAuthorizerAccessTokenResult> result =
                ThirdPartyApiWrapper.GetAuthorizerAccessToken(wrapper.AppId, wrapper.RefreshToken);

            if (result.Success)
            {
                wrapper.RefreshToken          = result.ApiResult.RefreshToken;
                wrapper.AccessToken           = result.ApiResult.AccessToken;
                wrapper.AccessTokenExpiryTime = DateTime.Now.AddSeconds(result.ApiResult.ExpiresIn);
            }
        }