public static string GetUserOpenID(string code) { string openid = ""; try { Log4Helper.WriteLog("appid=" + WeChatCommon.appid); Log4Helper.WriteLog("secret=" + WeChatCommon.secret); //获取token string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + WeChatCommon.appid + "&secret=" + WeChatCommon.secret + "&code=" + code + "&grant_type=authorization_code"; string jsonText = SendWX(url); Log4Helper.WriteLog(jsonText); JObject obj = (JObject)JsonConvert.DeserializeObject(jsonText); if (obj != null && !string.IsNullOrEmpty(ConvertHelper.ToString(obj["openid"]))) { openid = ConvertHelper.ToString(obj["openid"]); } } catch (Exception ex) { Log4Helper.WriteLog("获取OpenID失败", ex); } return(openid); }
/// <summary> /// 受影响行数 /// </summary> /// <param name="sql"></param> /// <returns></returns> public int Execute(string sql, SqlParameter[] parameters) { int result = 0; try { using (SqlConnection conn = new SqlConnection(connectionStr)) { if (conn.State != ConnectionState.Open) { conn.Open(); } using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = sql; cmd.Parameters.AddRange(parameters); result = cmd.ExecuteNonQuery(); } } } catch (Exception ex) { Log4Helper.WriteLog("执行sql错误", ex); } return(result); }
/// <summary> /// 消息推送 /// </summary> public static void SendTips(string openID, string title, string tipsName , string content, string remark) { JObject obj = JObject.FromObject(new { touser = openID, template_id = "2RUDu29ANeKhqFttNDRnS2nbHp63ztERPileyzmDUbs", data = JObject.FromObject(new { first = JObject.FromObject(new { value = tipsName, color = "#c20c0c" }), keyword1 = new { value = tipsName, color = "#173177" }, keyword2 = new { value = content, color = "#173177" }, remark = new { value = remark, color = "#173177" } }) }); Log4Helper.WriteLog(WeChatAPI.SendTemplateMsg(obj.ToString())); }
/// <summary> /// 验证码推送 /// </summary> /// <param name="openID"></param> /// <param name="message"></param> public static void LoginCodePush(string openID, string code) { JObject obj = JObject.FromObject(new { touser = openID, template_id = "2RUDu29ANeKhqFttNDRnS2nbHp63ztERPileyzmDUbs", data = JObject.FromObject(new { first = JObject.FromObject(new { value = "收到登录验证码", color = "#c20c0c" }), keyword1 = new { value = "登录验证码", color = "#173177" }, keyword2 = new { value = $"验证码:{code}", color = "#173177" }, remark = new { value = "发送时间:" + DateTime.Now.ToString(), color = "#173177" } }) }); Log4Helper.WriteLog(WeChatAPI.SendMessage("http://3861520.cn/Interface/MessagePush.asmx/ETPush", openID, "", "收到登录验证码", $"验证码:{code}", "发送时间:" + DateTime.Now.ToString())); }
public static object GetSDKConfig(string currentUrl, out int resultCode) { resultCode = OperationWidget.Failed; try { //时间戳 string timestamp = GeneralHelper.GetSecondsTimestamp(); //随机字符串 string nonceStr = GeneralHelper.GenerateRandomStr(16); //jsapi_ticket var result = GetJsApi_Ticket(); if (result.Status) { Hashtable hht = new Hashtable(); hht.Add("noncestr", nonceStr); hht.Add("jsapi_ticket", result.Data); hht.Add("timestamp", timestamp); hht.Add("url", currentUrl); StringBuilder sb = new StringBuilder(); ArrayList akeys = new ArrayList(hht.Keys); akeys.Sort(); foreach (string k in akeys) { string v = (string)hht[k]; sb.Append(k + "=" + v + "&"); } string tmpStr = sb.ToString().TrimEnd(new char[] { '&' }); tmpStr = WeChatCommon.GetSha1(tmpStr).ToLower(); var data = new { WeChatCommon.appid, timestamp, nonceStr, signature = tmpStr }; resultCode = OperationWidget.Success; return(data); } else { return(new { }); } } catch (Exception ex) { Log4Helper.WriteLog("获取jssdk配置信息", ex); return(new { }); } }
/// <summary> /// 将制定用户从在线列表中中删除 /// </summary> /// <param name="uid"></param> /// <returns></returns> public bool Remove(string uid) { List <string> keys; User user; try { bool isDelA = users.TryRemove(uid, out user); bool isDelB = usersAddress.TryRemove(uid, out keys); return(isDelA && isDelB); } catch (Exception e) { Log4Helper.WriteLog(typeof(UserManager), e); return(false); } }
/// <summary> /// 将指定用户添加到在线用户信息列表 /// </summary> /// <param name="user"></param> /// <returns></returns> public bool Add(User user) { try { User userer = users.GetOrAdd(user.Uid, user); List <string> list = usersAddress.GetOrAdd(user.Uid, new List <string>()); lock (list) { list.Add(user.ConnectionId); } } catch (Exception e) { Log4Helper.WriteLog(typeof(UserManager), e); return(false); } return(true); }
public static void Request(string url) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Timeout = 6000; request.KeepAlive = false; request.Method = "GET"; request.ServicePoint.ConnectionLimit = 512; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)"; // request.AllowAutoRedirect = true; //是否允许302 HttpWebResponse response = null; Stream stream = null; try { response = request.GetResponse() as HttpWebResponse; stream = response.GetResponseStream(); successcount++; Console.WriteLine("{0},请求成功!,成功次数:{1}", DateTime.Now, successcount); } catch (Exception e) { string base404 = "iVBORw0KGgoAAAANSUhEUgAAAQAAAAEAAQMAAABmvDolAAAAA1BMVEX8+fKNaX6qAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAH0lEQVRo3u3BAQ0AAADCoPdPbQ43oAAAAAAAAAAAvg0hAAABfxmcpwAAAABJRU5ErkJggg=="; byte[] bytes = Convert.FromBase64String(base404); Stream strem = new MemoryStream(bytes); errorcount++; Log4Helper.WriteLog(url + "\n"); Console.WriteLine("{0},请求失败!,失败次数:{1}", DateTime.Now, errorcount); } finally { request.Abort(); if (response != null) { response.Close(); } if (stream != null) { stream.Close(); } } }
/// <summary> /// 删除指定用户的链接地址 /// </summary> /// <param name="uid"></param> /// <param name="address"></param> /// <returns></returns> public bool Remove(string uid, string address) { lock (lockobj) { try { bool isDel = usersAddress[uid].Remove(address); if (usersAddress[uid].Count == 0) { return(Remove(uid)); } return(isDel); } catch (Exception e) { Log4Helper.WriteLog(typeof(UserManager), e); return(false); } } }
/// <summary> /// 获取token /// </summary> /// <returns></returns> public static string GetToken() { string access_token = ""; string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + WeChatCommon.appid + "&secret=" + WeChatCommon.secret; string jsonText = SendWX(url); JObject obj = (JObject)JsonConvert.DeserializeObject(jsonText); try { if (obj != null && !string.IsNullOrEmpty(ConvertHelper.ToString(obj["access_token"]))) { access_token = obj["access_token"].ToString(); } } catch (Exception ex) { Log4Helper.WriteLog("GetToken错误", ex); } return(access_token); }
// return user; //} public static string SendWX(string strUrl) { string content = ""; try { HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(strUrl); //用GET形式请求指定的地址 req.Method = "GET"; using (WebResponse wr = req.GetResponse()) { StreamReader reader = new StreamReader(wr.GetResponseStream(), Encoding.UTF8); content = reader.ReadToEnd(); reader.Close(); reader.Dispose(); } } catch (Exception ex) { Log4Helper.WriteLog("SendWX错误", ex); } return(content); }
protected void Application_Error() { Exception ex = Server.GetLastError().GetBaseException(); Log4Helper.WriteLog("程序错误", ex); }