/// <summary> /// 获取access_token(同时验证APPID) /// </summary> /// <param name="_AppID">第三方用户唯一凭证</param> /// <param name="_AppSecret">第三方用户唯一凭证密钥</param> /// <returns>返回菜单ACCESS_TOKEN</returns> public static string ResetAccessToken(string _AppID, string _AppSecret) { Shove._IO.Log log = new _IO.Log("WeixinGongzhong"); if (String.IsNullOrEmpty(_AppID) || String.IsNullOrEmpty(_AppSecret)) { log.Write("从微信服务器获取 access_token 发生错误:未提供有效的 AppID、AppSecret,请通过 Utility.InitializeAccessToken(AppID, AppSecret) 方法提供参数后再进行接口访问。"); return(""); } WebClient webClient = new WebClient(); Byte[] bytes = null; try { bytes = webClient.DownloadData( string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", _AppID, _AppSecret)); } catch (Exception e) { log.Write("从微信服务器获取 access_token 发生错误:" + e.Message); return(""); } finally { webClient.Dispose(); } if (bytes == null || bytes.Length == 0) { log.Write("从微信服务器获取 access_token 发生错误"); return(""); } string result = System.Text.Encoding.UTF8.GetString(bytes); if (result.IndexOf("errcode") >= 0) { log.Write("从微信服务器获取 access_token 发生错误:" + result); return(""); } string[] res = result.Split('\"'); //获取的access_token access_token = res[3].ToString(); //更新access_token过期时间 last_token_datetime = DateTime.Now; return(access_token); }
private static void WriteResponse(string checkType) { string result = "系统检测到您提交的数据中存在恶意的注入型攻击数据(或 img 标签的 src 文件不合法),请检查你的 " + checkType + " 数据,如果是系统误报,请联系我们处理,谢谢。给您带来了不便,十分抱歉!【技术支持:深圳英迈思文化科技有限公司·EIMS 研究院·云计算实验室与晓风系列产品支撑中心】"; log.Write("InjectionInterceptorError: " + result + "\r\n" + HttpContext.Current.Request.Url.AbsoluteUri); // throw new Exception(result); // 本来是应该直接 throw 的,但是应用层面可能会有程序员这么做: // 1、将调用拦截器的代码 try 起来。那就没有作用了。 // 2、throw 是页面错误代码 500,有些程序将 500 做了重定向,重定向到专用的错误页面了,这样有些扫描工具会误报。 HttpContext.Current.Response.StatusCode = 412; HttpContext.Current.Response.Write(result); HttpContext.Current.Response.End(); }
/// <summary> /// 获取用户的基本信息 /// </summary> /// <param name="OpenId">用户账号的唯一标识ID</param> /// <param name="lang">返回国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语 为空默认中文简体</param> /// <param name="errorDescription">错误描述</param> /// <returns>返回用户基本信息实体UserInformation</returns> public static UserInformation GetUserInformation(string OpenId, string lang, ref string errorDescription) { Shove._IO.Log log = new _IO.Log("WeixinGongzhong"); errorDescription = ""; lang = string.IsNullOrEmpty(lang) ? "zh_CN" : lang.Trim(); if (string.IsNullOrEmpty(OpenId)) { errorDescription = "OpenId不正确,null"; return(null); } if (string.IsNullOrEmpty(Utility.Access_token)) { errorDescription = "发生错误:access_token为null,请查看页面的Load事件是否调用了Utility.GetAccessToken()方法"; return(null); } string errorCode = ""; using (System.Net.WebClient client = new System.Net.WebClient()) { try { byte[] by = client.DownloadData(string.Format("https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang={2}", Utility.Access_token, OpenId, lang)); errorCode = System.Text.Encoding.UTF8.GetString(by); } catch (Exception ex) { errorDescription = ex.Message; return(null); } } if (errorCode.IndexOf("errcode") >= 0) { errorDescription = ErrorInformation.GetErrorCode(errorCode, "errcode"); log.Write(errorDescription + "\t" + Utility.Access_token + "\t" + Utility.AppID + "\t" + Utility.AppSecret); return(null); } return(JsonToObject(errorCode, ref errorDescription)); }