/// <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) { WXBizMsgCrypt wxcpt = new 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); }
/// <summary> /// 解密消息 /// </summary> /// <returns>返回解密之后的消息</returns> private string DecryptMessage(string content) { string msg = ""; AccountInfo account = AccountInfoCollection.GetAccountInfo("zizaifan"); if (account == null) { return(msg); } WXBizMsgCrypt wxcpt = new 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); }
/// <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); }
private void BasePage_Load(object sender, EventArgs e) { //初始化公众号信息 AccountInfoCollection.SetAccountInfo(new AccountInfo("微信号", "AppID(应用ID)", "AppSecret(应用密钥)", "Token(令牌)", "EncodingAESKey(消息加解密密钥)", "自在饭")); }