示例#1
0
        /// <summary>
        /// 返回XML格式的响应消息
        /// </summary>
        /// <param name="encryptType">消息加密类型</param>
        /// <returns>返回XML格式的响应消息</returns>
        public string ToXml(MessageEncryptTypeEnum encryptType)
        {
            int WXBizMsgCrypt_OK = 0;
            //得到未加密的XML响应消息
            string xml = ToXml();

            //如果需要加密,加密消息
            if (encryptType == MessageEncryptTypeEnum.aes)
            {
                int         timeStamp = Utility.ToWeixinTime(CreateTime);
                Random      random    = new Random();
                string      nonce     = random.Next().ToString();
                AccountInfo account   = AccountInfoCollection.GetAccountInfo(FromUserName);
                if (account != null)
                {
                    Tencent.WXBizMsgCrypt wxcpt = new Tencent.WXBizMsgCrypt(account.Token, account.EncodingAESKey, account.AppId);
                    string xmlEncrypt           = "";
                    //加密消息
                    if (wxcpt.EncryptMsg(xml, timeStamp.ToString(), nonce, ref xmlEncrypt) == WXBizMsgCrypt_OK)
                    {
                        return(xmlEncrypt);
                    }
                }
            }
            return(xml);
        }
        /// <summary>
        /// 获取用户网页授权的地址
        /// </summary>
        /// <param name="userName">公众号</param>
        /// <param name="redirectUrl">用户授权之后的回调地址</param>
        /// <param name="scope">应用授权作用域</param>
        /// <param name="state">重定向之后的state参数</param>
        /// <returns>返回用户网页授权的地址;如果获取公众号信息失败或者参数错误,返回空字符串。</returns>
        public static string GetOAuthUrl(string userName, string redirectUrl, OAuthScopeEnum scope, string state = null)
        {
            string      url     = string.Empty;
            AccountInfo account = AccountInfoCollection.GetAccountInfo(userName);

            if (account == null)
            {
                return(url);
            }
            if (string.IsNullOrWhiteSpace(redirectUrl))
            {
                return(url);
            }
            if (state == null)
            {
                state = string.Empty;
            }
            if (state.Length > maxStateLength)
            {
                return(url);
            }
            foreach (char c in state)
            {
                if (!char.IsLetterOrDigit(c))
                {
                    return(url);
                }
            }
            url = string.Format(urlForGettingOAuthUrl, account.AppId, HttpUtility.UrlEncode(redirectUrl), scope.ToString("g"), state);
            return(url);
        }
示例#3
0
        /// <summary>
        /// 解密消息
        /// </summary>
        /// <returns>返回解密之后的消息</returns>
        public string DecryptMessage(string content)
        {
            string      msg = "";
            XmlDocument doc = new XmlDocument();

            doc.LoadXml(content);
            XmlNode     root     = doc.FirstChild;
            string      userName = root["ToUserName"].InnerText;
            AccountInfo account  = AccountInfoCollection.GetAccountInfo(userName);

            if (account == null)
            {
                return(msg);
            }
            Tencent.WXBizMsgCrypt wxcpt = new Tencent.WXBizMsgCrypt(account.Token, account.EncodingAESKey, account.AppId);
            string msg_signature        = RequestEx.TryGetQueryString("msg_signature", "", request);
            string timestamp            = RequestEx.TryGetQueryString("timestamp", "", request);
            string nonce = RequestEx.TryGetQueryString("nonce", "", request);

            wxcpt.DecryptMsg(msg_signature, timestamp, nonce, content, ref msg);
            return(msg);
        }
示例#4
0
        /// <summary>
        /// 从微信服务器获取access token
        /// </summary>
        /// <param name="userName">公众号</param>
        /// <param name="msg">从服务器返回的错误信息。</param>
        /// <returns>返回许可令牌;如果获取失败,返回null。</returns>
        private static AccessToken GetFromWeixinServer(string userName, out ErrorMessage msg)
        {
            AccessToken token = null;

            msg = new ErrorMessage(ErrorMessage.ExceptionCode, "");
            AccountInfo account = AccountInfoCollection.GetAccountInfo(userName);

            if (account == null)
            {
                msg.errmsg = "获取公众号参数失败。";
                return(token);
            }
            string url = string.Format(urlForGettingAccessToken, account.AppId, account.AppSecret);
            string result;

            if (!HttpHelper.Request(url, out result, httpMethodForGettingAccessToken, string.Empty))
            {
                msg.errmsg = "从微信服务器获取响应失败。";
                return(token);
            }
            if (ErrorMessage.IsErrorMessage(result))
            {
                msg = ErrorMessage.Parse(result);
            }
            else
            {
                try
                {
                    token = AccessToken.ParseFromJson(result);
                }
                catch (Exception e)
                {
                    msg = new ErrorMessage(e);
                }
            }
            return(token);
        }
        /// <summary>
        /// 刷新access token
        /// </summary>
        /// <param name="userName">公众号</param>
        /// <param name="refreshToken">用户刷新token</param>
        /// <param name="errorMessage">返回获取是否成功</param>
        /// <returns>返回access token;如果获取失败,返回null。</returns>
        public static OAuthAccessToken Refresh(string userName, string refreshToken, out ErrorMessage errorMessage)
        {
            OAuthAccessToken token = null;

            if (string.IsNullOrWhiteSpace(refreshToken))
            {
                errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "用户刷新token不能为空。");
                return(token);
            }
            AccountInfo account = AccountInfoCollection.GetAccountInfo(userName);

            if (account == null)
            {
                errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "获取公众号信息失败。");
                return(token);
            }
            string url = string.Format(urlForRefreshingAccessToken, account.AppId, refreshToken);
            string responseContent;

            if (!HttpHelper.Request(url, out responseContent, httpMethod, (string)null))
            {
                errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "从微信服务器获取响应失败。");
            }
            else if (ErrorMessage.IsErrorMessage(responseContent))
            {
                errorMessage = ErrorMessage.Parse(responseContent);
            }
            else
            {
                var result = JsonConvert.DeserializeAnonymousType(responseContent,
                                                                  new { access_token = "", expires_in = 0, refresh_token = "", openid = "", scope = "" });
                token        = new OAuthAccessToken(result.access_token, result.expires_in, result.refresh_token, result.openid, result.scope);
                errorMessage = new ErrorMessage(ErrorMessage.SuccessCode, "获取access token成功。");
            }
            return(token);
        }