protected override void ProcessRequestPath(WebParameter p, GoData d) { var context = p.CurrentHttpContext; //抓取请求资源 p.RequestResourcePath = ComFunc.nvl(context.Request.Path).Replace("\\", "/").Replace(weixinroothome.Replace("~", ""), ""); if (p.RequestResourceName == "" && p.RequestResourcePath == "/") { p.RequestResourcePath = p.RequestResourcePath + weixinhome; } var ext = Path.GetExtension(p.RequestResourcePath); if (ext != "") { string reqpath = Path.GetFileNameWithoutExtension(p.RequestResourcePath); string[] ss = reqpath.Split('.'); p.Action = ss.Length > 1 ? ss[1] : ""; p.RequestResourceName = ss.Length > 0 ? ss[0] : ""; p.RequestResources = ss; } else { var turl = p.RequestResourcePath.Replace("~", ""); turl = turl.StartsWith("/") ? turl.Substring(1) : turl; string[] ss = turl.Split('/'); p.Action = ss.Length > 1 ? ss[1] : ""; p.RequestResourceName = ss.Length > 0 ? ss[0] : ""; p.RequestResources = ss; } }
protected override void InvokeAction(WebParameter p, GoData d) { if (IsValid4Invoke(p, d)) { base.InvokeAction(p, d); } }
/// <summary> /// 呼叫本地logic /// </summary> /// <param name="logic"></param> /// <param name="action"></param> /// <param name="p"></param> /// <param name="d"></param> /// <returns></returns> private object CallLocalLogic(string logic, string action, WebParameter p, GoData d) { object od = d; GlobalCommon.Proxys["gobusi"].CallModule(ref od, p); return(d.ResponseData); }
protected override bool IsValid4Invoke(WebParameter p, GoData d) { var rtn = true; // if (!GlobalPrepare.IsIgnoreLoginAuth(p)) // { // if (p.LoginInfo == null) // { // rtn = false; // if(ComFunc.nvl(p.CurrentHttpContext.Request.Headers["x-requested-with"].FirstOrDefault()) == "XMLHttpRequest" // && ComFunc.nvl(p.CurrentHttpContext.Request.Headers["x-request-async"].FirstOrDefault()) == "true") // { // d.ResponseData = FrameDLRObject.CreateInstance(@"{ //__isneedlogin__:true, //__loginurl__:'/admin' //}"); // } // else // { // d.ResponseData = FrameDLRObject.CreateInstance(); // d.RedirectUri = "/admin"; // } // } // } return(rtn); }
protected override ParameterStd ConvertParameters(object[] obj) { var fea = FrameExposedArray.From(obj); var rtn = new WebParameter(); rtn.CurrentHttpContext = fea.defaulthttpcontext.value; return(rtn); }
private void DebugLog(string msg, WebParameter p) { var isdebug = p[DomainKey.CONFIG, "DebugMode"] == null ? false : (bool)p[DomainKey.CONFIG, "DebugMode"]; if (isdebug) { GlobalCommon.Logger.WriteLog(LoggerLevel.DEBUG, msg); } }
public static void RenderView(WebParameter wp, WMvcData wd, HttpContext context, TextWriter writer) { RouteData rd = new RouteData(); ControllerContext cc = new ControllerContext();//new ControllerContext(new HttpContextWrapper(context), new RouteData(), new MyController()); cc.HttpContext = new HttpContextWrapper(context); cc.RouteData = new RouteData(); cc.RouteData.Values.Add("controller", wp.RequestResourceName); cc.RouteData.Values.Add("action", "Process"); //添加Mvc Module数据 ViewDataDictionary vdd = null; if (wd.MvcModuleData != null) { vdd = new ViewDataDictionary(wd.MvcModuleData); } else { vdd = new ViewDataDictionary(); } //添加ViewData foreach (var val in wd.Domain(DomainKey.VIEW_LIST)) { if (vdd.ContainsKey(val.Key)) { vdd[val.Key] = val.Value; } else { vdd.Add(val.Key, val.Value); } } TempDataDictionary tdd = new TempDataDictionary(); if (string.IsNullOrEmpty(wd.ViewPath)) { throw new Exception("没有获得ViewPath,无法展现页面"); } WMvcView rv = new WMvcView(wd.ViewPath); TextWriter _tw = new StringWriter(); try { ViewContext vc = new ViewContext(cc, rv, vdd, tdd, _tw); rv.Render(vc, writer); } finally { _tw.Close(); _tw.Dispose(); _tw = null; vdd.Clear(); rv.Dispose(); } }
/// <summary> /// 读取request中的参数写入webparameter中 /// </summary> /// <typeparam name="WP"></typeparam> /// <param name="request"></param> /// <param name="wp"></param> public static void LoadEFFCParameters <WP>(this HttpRequest request, ref WP wp) where WP : WebParameter { if (wp == null) { wp = Activator.CreateInstance <WP>(); } WebParameter p = wp; request.LoadEFFCParameters(ref p); }
/// <summary> /// 业务逻辑执行之前的检核判定,如果为true,则执行业务逻辑模块,否则不执行 /// </summary> /// <param name="p"></param> /// <param name="d"></param> /// <returns></returns> protected virtual bool IsValid4Invoke(WebParameter p, GoData d) { var isrun = true; if (!IsWeixinSignValid(p, d)) { isrun = false; } return(isrun); }
protected override void InvokeAction(WebParameter p, GoData d) { if (IsValid4Invoke(p, d)) { base.InvokeAction(p, d); } else { throw new Exception("无效访问,可能原因,缺少访问授权信息或密钥不正确"); } }
protected override void FinishedProcess(WebParameter p, GoData d) { base.FinishedProcess(p, d); var logkey = ComFunc.nvl(p.GetValue("logkey")); if (logmsg.ContainsKey(logkey)) { var dobj = (FrameDLRObject)logmsg[logkey]; DebugLog(string.Format("标号{0}微信请求处理记录:\n{1}", logkey, dobj.ToJSONString()), p); } }
public string GetValue(List <WebParameter> lst, string key) { WebParameter param = new WebParameter(); for (int i = 0; i < lst.Count; i++) { if (lst[i].Key == key) { param = lst[i]; break; } } return(param.Value); }
protected override void SetResponseContent(WebParameter p, GoData d) { if (d.ResponseData is FrameDLRObject) { var re = (FrameDLRObject)d.ResponseData; var content = ToXml(re); if (p.ExtentionObj.weixin.encrypt_type == "aes") { var createTime = re.GetValue("CreateTime") == null ? DateTime.Now : (DateTime)re.GetValue("CreateTime"); int timeStamp = ToWeixinTime(createTime); Random random = new Random(); string nonce = random.Next().ToString(); WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(p.ExtentionObj.weixin.token, p.ExtentionObj.weixin.encrypt_key, p.ExtentionObj.weixin.appid); string xmlEncrypt = ""; //加密消息 if (wxcpt.EncryptMsg(content, timeStamp.ToString(), nonce, ref xmlEncrypt) == WXBizMsgCrypt.WXBizMsgCryptErrorCode.WXBizMsgCrypt_OK) { content = xmlEncrypt; } } var msgbytelength = Encoding.UTF8.GetByteCount(content); CurrentContext.Response.Headers.Add("Content-Length", msgbytelength + ""); CurrentContext.Response.ContentType = ResponseHeader_ContentType.xml + ";charset=utf-8"; CurrentContext.Response.StatusCode = 200; CurrentContext.Response.WriteAsync(content); } else { var msgbytelength = Encoding.UTF8.GetByteCount(ComFunc.nvl(d.ResponseData)); CurrentContext.Response.Headers.Add("Content-Length", msgbytelength + ""); if (d.ContentType == GoResponseDataType.String) { CurrentContext.Response.ContentType = ResponseHeader_ContentType.html + ";charset=utf-8"; } else { CurrentContext.Response.ContentType = ResponseHeader_ContentType.xml + ";charset=utf-8"; } CurrentContext.Response.StatusCode = 200; CurrentContext.Response.WriteAsync(ComFunc.nvl(d.ResponseData)); } }
protected override bool IsValid4Invoke(WebParameter p, GoData d) { var rtn = base.IsValid4Invoke(p, d); if (!rtn) { return(rtn); } var ext = Path.GetExtension(CurrentContext.Request.Path).Replace(".", "").ToLower(); if (ext == "go") { } return(rtn); }
private bool IsWeixinSignValid(WebParameter p, GoData d) { var rtn = true; string token = p.ExtentionObj.weixin.token; string signature = p.ExtentionObj.weixin.signature; string timestamp = p.ExtentionObj.weixin.timestamp; string nonce = p.ExtentionObj.weixin.nonce; if (string.IsNullOrWhiteSpace(signature) || string.IsNullOrWhiteSpace(timestamp) || string.IsNullOrWhiteSpace(nonce)) { rtn = false; var dobj = FrameDLRObject.CreateInstance(FrameDLRFlags.SensitiveCase); dobj.ToUserName = p[DomainKey.POST_DATA, "FromUserName"]; dobj.FromUserName = p[DomainKey.POST_DATA, "ToUserName"]; dobj.CreateTime = DateTime.Now; dobj.MsgType = "text"; dobj.Content = "检验无效,微信请求参数不正确"; dobj.FuncFlag = 0; d.ResponseData = dobj; } else { string[] infos = new string[] { token, timestamp, nonce }; Array.Sort <string>(infos); string info = string.Format("{0}{1}{2}", infos[0], infos[1], infos[2]); rtn = string.Compare(signature, GetSha1Hash(info, Encoding.ASCII), true) == 0; if (!rtn) { var dobj = FrameDLRObject.CreateInstance(FrameDLRFlags.SensitiveCase); dobj.ToUserName = p[DomainKey.POST_DATA, "FromUserName"]; dobj.FromUserName = p[DomainKey.POST_DATA, "ToUserName"]; dobj.CreateTime = DateTime.Now; dobj.MsgType = "text"; dobj.Content = "检验无效,不是微信访问接口"; dobj.FuncFlag = 0; d.ResponseData = dobj; } } return(rtn); }
/// <summary> /// 判断当前请求是否忽略登录验证 /// </summary> /// <param name="p"></param> /// <returns></returns> public static bool IsIgnoreLoginAuth(WebParameter p) { if (_ignorelist == null) { _ignorelist = new List <string>(); _ignorelist.Add("admin"); _ignorelist.Add("admin/login"); _ignorelist.Add("admin/logout"); } var s = p.RequestResourceName + (p.Action == "" ? "" : $"/{p.Action}"); if (!_ignorelist.Contains(s.ToLower()) && !_ignorelist.Contains($"{p.RequestResourceName.ToLower()}/*")) { return(false); } else { return(true); } }
protected override void LoadConfig(WebParameter p, GoData d) { base.LoadConfig(p, d); bool bvalue = true; foreach (var item in MyConfig.GetConfigurationList("Weixin")) { if (bool.TryParse(ComFunc.nvl(item.Value), out bvalue)) { p[DomainKey.CONFIG, item.Key] = bool.Parse(ComFunc.nvl(item.Value)); } else if (DateTimeStd.IsDateTime(item.Value)) { p[DomainKey.CONFIG, item.Key] = DateTimeStd.ParseStd(item.Value).Value; } else { p[DomainKey.CONFIG, item.Key] = ComFunc.nvl(item.Value); } } }
protected override void ProcessRequestInfo(WebParameter p, GoData d) { base.ProcessRequestInfo(p, d); //微信相关信息 p.ExtentionObj.weixin = FrameDLRObject.CreateInstance(FrameDLRFlags.SensitiveCase); p.ExtentionObj.weixin.signature = ComFunc.nvl(p[DomainKey.QUERY_STRING, "signature"]); p.ExtentionObj.weixin.timestamp = ComFunc.nvl(p[DomainKey.QUERY_STRING, "timestamp"]); p.ExtentionObj.weixin.nonce = ComFunc.nvl(p[DomainKey.QUERY_STRING, "nonce"]); p.ExtentionObj.weixin.token = ComFunc.nvl(p[DomainKey.CONFIG, "weixin_token"]); p.ExtentionObj.weixin.encrypt_type = ComFunc.nvl(p[DomainKey.QUERY_STRING, "encrypt_type"]); p.ExtentionObj.weixin.encrypt_key = ComFunc.nvl(p[DomainKey.CONFIG, "weixin_encry_key"]); p.ExtentionObj.weixin.appid = ComFunc.nvl(p[DomainKey.CONFIG, "weixin_Appid"]); p.ExtentionObj.weixin.appsecret = ComFunc.nvl(p[DomainKey.CONFIG, "weixin_Appsecret"]); p.ExtentionObj.weixin.weixin_mch_ssl_path = ComFunc.nvl(p[DomainKey.CONFIG, "weixin_Mch_SSL_Path"]); p.ExtentionObj.weixin.weixin_mch_ssl_pass = ComFunc.nvl(p[DomainKey.CONFIG, "weixin_Mch_SSL_Pass"]); p.ExtentionObj.weixin.agentid = ComFunc.nvl(p[DomainKey.CONFIG, "weixin_AgentId"]); p.ExtentionObj.weixinmp = FrameDLRObject.CreateInstance(FrameDLRFlags.SensitiveCase); p.ExtentionObj.weixinmp.appid = ComFunc.nvl(p[DomainKey.CONFIG, "weixinmp_Appid"]); p.ExtentionObj.weixinmp.appsecret = ComFunc.nvl(p[DomainKey.CONFIG, "weixinmp_Appsecret"]); p.ExtentionObj.weixinmp.weixinmp_mch_ssl_path = ComFunc.nvl(p[DomainKey.CONFIG, "weixinmp_Mch_SSL_Path"]); p.ExtentionObj.weixinmp.weixinmp_mch_ssl_pass = ComFunc.nvl(p[DomainKey.CONFIG, "weixinmp_Mch_SSL_Pass"]); }
public List <WebParameter> GetAll() { List <WebParameter> lst = new List <WebParameter>(); var configuration = WebConfigurationManager.OpenWebConfiguration("~"); // var section = (AppSettingsSection)configuration.GetSection("appSettings"); foreach (KeyValueConfigurationElement setting in configuration.AppSettings.Settings) { if (setting.Key == "webpages:Version" || setting.Key == "webpages:Enabled" || setting.Key == "ClientValidationEnabled" || setting.Key == "UnobtrusiveJavaScriptEnabled") { continue; } WebParameter param = new WebParameter { Key = setting.Key, Value = setting.Value }; lst.Add(param); } return(lst); }
public async Task <object> Execute(WebParameterCollection webParameters, bool isRegenAccessTokenValue = false) { if (_reader == null) { return(null); } IResponse resp = null; try { _refreshTokenValue = _getRefreshTokenValue == null ? null : _getRefreshTokenValue(); _accessTokenValue = _getAccessTokenValue == null ? null : _getAccessTokenValue(); webParameters.AccessValue = _accessTokenValue; webParameters.LangCode = _getLanguageCode == null ? "" : _getLanguageCode(); webParameters.LangCode = string.IsNullOrEmpty(webParameters.LangCode) ? "en" : webParameters.LangCode; resp = await _reader.Execute(webParameters); if (resp.Error != null) { if (resp.Error.GetErrorMsgID() == MessageIdentifier.ACCESS_CONNECTION_TOKEN_EXPIRED && !isRegenAccessTokenValue && webParameters != null) { if (webParameters.WebParameters == null) { webParameters.WebParameters = new WebParameter[0]; } webParameters.WebParameters = (new WebParameter[] { WebParameter.Create("RegenAccessTokenValue", new object[] { _refreshTokenValue }) }).Concat(webParameters.WebParameters).ToArray(); return(await Execute(webParameters, true)); } throw resp.Error.ToException(); } if (resp is JSonMultiResponse) { var mResp = (JSonMultiResponse)resp; if (mResp.Result != null && mResp.Result.Length > 0 && mResp.Result[0].MethodName == "RegenAccessTokenValue") { if (mResp.Result[0] == null) { throw new NullReferenceException("RegenAccessTokenValue"); } if (mResp.Result[0].Error != null) { throw mResp.Result[0].Error.ToException(); } var regenResp = mResp.Result[0]; if (regenResp.Result is AccessToken) { OnChangeAccessToken(((AccessToken)regenResp.Result).Value, ((AccessToken)regenResp.Result).InvalidPsswrdFormat); } mResp.Result = mResp.Result.Skip(1).ToArray(); if (mResp.Result.Length > 0 && mResp.Result[0].MethodName == "VerifyConnectionToken") { mResp.Result[0].Error = regenResp.Error; mResp.Result[0].Result = regenResp.Result; } regenResp = null; } if (mResp.Result != null && mResp.Result.Length == 1) { resp = mResp.Result[0]; if (resp.Error != null) { throw resp.Error.ToException(); } } mResp = null; } return(resp.Result); } catch (Exception ex) { throw ex; } finally { resp = null; } }
protected virtual void SetContent(WebParameter p, GoData d) { System.Collections.Specialized.NameValueCollection obj = CurrentContext.Request.Headers; String agent = ComFunc.nvl(obj["USER-AGENT"]); if (d.ResponseData == null) { throw new Exception("ResponseData is null!"); } if (d.ContentType == GoResponseDataType.Pic_Jpg) { byte[] b = null; if (d.ResponseData is Stream) { b = StreamToBytes((Stream)d.ResponseData); } else { b = ((byte[])d.ResponseData); } CurrentContext.Response.AppendHeader("Content-Length", b.Length + ""); CurrentContext.Response.ContentType = "image/jpeg"; CurrentContext.Response.BinaryWrite(b); } else if (d.ContentType == GoResponseDataType.Pic_Gif) { byte[] b = null; if (d.ResponseData is Stream) { b = StreamToBytes((Stream)d.ResponseData); } else { b = ((byte[])d.ResponseData); } CurrentContext.Response.AppendHeader("Content-Length", b.Length + ""); CurrentContext.Response.ContentType = "image/gif"; CurrentContext.Response.BinaryWrite(b); } else if (d.ContentType == GoResponseDataType.Pic_Bmp) { byte[] b = null; if (d.ResponseData is Stream) { b = StreamToBytes((Stream)d.ResponseData); } else { b = ((byte[])d.ResponseData); } CurrentContext.Response.AppendHeader("Content-Length", b.Length + ""); CurrentContext.Response.ContentType = "image/bmp"; CurrentContext.Response.BinaryWrite(b); } else if (d.ContentType == GoResponseDataType.Pic_png) { byte[] b = null; if (d.ResponseData is Stream) { b = StreamToBytes((Stream)d.ResponseData); } else { b = ((byte[])d.ResponseData); } CurrentContext.Response.AppendHeader("Content-Length", b.Length + ""); CurrentContext.Response.ContentType = "image/png"; CurrentContext.Response.BinaryWrite(b); } else if (d.ContentType == GoResponseDataType.Excel) { CurrentContext.Response.Buffer = true; CurrentContext.Response.Charset = "UTF-8"; //CurrentContext.Response.ClearHeaders(); CurrentContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(ComFunc.nvl(d["__download_filename__"]), Encoding.UTF8)); CurrentContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); CurrentContext.Response.ContentType = ResponseHeader_ContentType.xls; if (d.ResponseData is byte[]) { CurrentContext.Response.BinaryWrite((byte[])d.ResponseData); } else if (d.ResponseData is Stream) { CurrentContext.Response.BinaryWrite(StreamToBytes((Stream)d.ResponseData)); } else { CurrentContext.Response.Write(d.ResponseData); } CurrentContext.Response.Flush(); } else if (d.ContentType == GoResponseDataType.Word) { CurrentContext.Response.Buffer = true; CurrentContext.Response.Charset = "UTF-8"; //CurrentContext.Response.ClearHeaders(); CurrentContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(ComFunc.nvl(d["__download_filename__"]), Encoding.UTF8)); CurrentContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); CurrentContext.Response.ContentType = ResponseHeader_ContentType.doc; if (d.ResponseData is byte[]) { CurrentContext.Response.BinaryWrite((byte[])d.ResponseData); } else if (d.ResponseData is Stream) { CurrentContext.Response.BinaryWrite(StreamToBytes((Stream)d.ResponseData)); } else { CurrentContext.Response.Write(d.ResponseData); } CurrentContext.Response.Flush(); } else if (d.ContentType == GoResponseDataType.PDF) { CurrentContext.Response.Buffer = true; CurrentContext.Response.Charset = "UTF-8"; //CurrentContext.Response.ClearHeaders(); if (agent != null && agent.IndexOf("MSIE") == -1 && agent.IndexOf("Chrome") == -1 && agent.IndexOf("Opera") == -1) { //非IE非Chrom CurrentContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + ComFunc.nvl(d["__download_filename__"])); } else { CurrentContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(ComFunc.nvl(d["__download_filename__"]), Encoding.UTF8)); } CurrentContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); CurrentContext.Response.ContentType = ResponseHeader_ContentType.pdf; if (d.ResponseData is byte[]) { CurrentContext.Response.BinaryWrite((byte[])d.ResponseData); } else if (d.ResponseData is Stream) { CurrentContext.Response.BinaryWrite(StreamToBytes((Stream)d.ResponseData)); } else { CurrentContext.Response.Write(d.ResponseData); } CurrentContext.Response.Flush(); } else if (d.ContentType == GoResponseDataType.Json) { CurrentContext.Response.Charset = "UTF-8"; CurrentContext.Response.ContentType = ResponseHeader_ContentType.json; if (d.ResponseData is FrameDLRObject) { CurrentContext.Response.Write(ComFunc.FormatJSON((FrameDLRObject)d.ResponseData).ToJSONString()); } else { CurrentContext.Response.Write(ComFunc.FormatJSON(ComFunc.nvl(d.ResponseData)).ToJSONString()); } } else if (d.ContentType == GoResponseDataType.HostView) { if (d.ResponseData is FrameDLRObject) { var dobj = (FrameDLRObject)d.ResponseData; //获取view路径 string viewpath = ComFunc.nvl(d.ExtentionObj.hostviewpath); viewpath = viewpath.Replace("~", GlobalCommon.HostCommon.RootPath + HostJsConstants.COMPILED_VIEW_PATH); if (File.Exists(viewpath)) { //调用hostview引擎进行渲染 HostJsView hjv = (HostJsView)p.ExtentionObj.hostviewengine; hjv.CurrentContext.SetDataModel(dobj.ToDictionary()); var html = hjv.Render(File.ReadAllText(viewpath, Encoding.UTF8)); CurrentContext.Response.Charset = "UTF-8"; CurrentContext.Response.ContentType = ResponseHeader_ContentType.html; CurrentContext.Response.Write(html); } else { CurrentContext.Response.Write("File Not Found!"); } } } else if (d.ContentType == GoResponseDataType.RazorView) { //Mvc进行视图展示 StringWriter sw = new StringWriter(); WMvcView.RenderView(p, d, CurrentContext, sw); d.SetValue("ViewHtmlCode", sw.ToString()); if (!IsWebSocket) { CurrentContext.Response.Charset = "UTF-8"; CurrentContext.Response.ContentType = ResponseHeader_ContentType.html; CurrentContext.Response.Write(sw.ToString()); } else { CurrentContext.Response.Charset = "UTF-8"; CurrentContext.Response.ContentType = ResponseHeader_ContentType.json; var v = ComFunc.FormatJSON(sw.ToString()); var buffer = new ArraySegment <byte>(Encoding.UTF8.GetBytes(v.ToJSONString())); CurrentSocket.SendAsync(buffer, WebSocketMessageType.Text, true, System.Threading.CancellationToken.None); } } else { CurrentContext.Response.Write(d.ResponseData); } }
/// <summary> /// 业务逻辑执行之前的检核判定,如果为true,则执行业务逻辑模块,否则不执行 /// </summary> /// <param name="p"></param> /// <param name="d"></param> /// <returns></returns> protected virtual bool IsValid4Invoke(WebParameter p, GoData d) { return(true); }
/// <summary> /// 读取request中的参数写入webparameter中 /// </summary> /// <param name="request"></param> /// <param name="wp"></param> public static void LoadEFFCParameters(this HttpRequest request, ref WebParameter wp) { if (wp == null) { wp = new WebParameter(); } wp.RequestMethod = request.Method; var contenttype = ComFunc.nvl(request.ContentType).ToLower(); contenttype = contenttype == "" ? "text/plain" : contenttype; foreach (var item in request.Query) { wp[DomainKey.QUERY_STRING, item.Key] = item.Value.ToString(); } GlobalCommon.Logger.WriteLog(LoggerLevel.DEBUG, $"request contenttype:{contenttype}"); if (request.Method.ToLower() == "post" || request.Method.ToLower() == "put" || request.Method.ToLower() == "patch" || request.Method.ToLower() == "delete") { using (var stream = new MemoryStream()) { request.Body.CopyTo(stream); stream.Seek(0, SeekOrigin.Begin); if (contenttype.IndexOf("/json") > 0) { var sr = new StreamReader(stream); var str = sr.ReadToEnd(); GlobalCommon.Logger.WriteLog(LoggerLevel.DEBUG, $"request content:{str}"); if (!string.IsNullOrEmpty(str)) { FrameDLRObject o = null; FrameDLRObject.TryParse(str, FrameDLRFlags.SensitiveCase, out o); if (o != null) { foreach (var k in o.Keys) { wp[DomainKey.POST_DATA, k] = o.GetValue(k); } } } wp.RequestContent = str; } //xml if (contenttype.IndexOf("/xml") > 0) { var sr = new StreamReader(stream); var str = sr.ReadToEnd(); GlobalCommon.Logger.WriteLog(LoggerLevel.DEBUG, $"request content:{str}"); if (!string.IsNullOrEmpty(str)) { FrameDLRObject o = FrameDLRObject.CreateInstance(str, FrameDLRFlags.SensitiveCase); if (o != null) { foreach (var k in o.Keys) { wp[DomainKey.POST_DATA, k] = o.GetValue(k); } } } wp.RequestContent = str; } //multipart/form-data if (contenttype.IndexOf("multipart/form-data") >= 0) { var mp = ParseMultipartFormData(stream, Encoding.UTF8); stream.Seek(0, SeekOrigin.Begin); GlobalCommon.Logger.WriteLog(LoggerLevel.DEBUG, $"request content:{new StreamReader(stream).ReadToEnd()}"); foreach (var k in mp.Keys) { if (mp.GetValue(k) is FrameUploadFile) { wp[DomainKey.UPDATE_FILE, k] = mp.GetValue(k); } else { wp[DomainKey.POST_DATA, k] = mp.GetValue(k); } } } if (contenttype.StartsWith("text/")) { var sr = new StreamReader(stream); var str = sr.ReadToEnd(); GlobalCommon.Logger.WriteLog(LoggerLevel.DEBUG, $"request content:{str}"); if (!string.IsNullOrEmpty(str)) { var sarr = QueryHelpers.ParseQuery(str); if (sarr != null) { foreach (var k in sarr) { wp[DomainKey.POST_DATA, k.Key] = k.Value.ToString(); } } } } if (contenttype.IndexOf("application/x-www-form-urlencoded") >= 0) { var sr = new StreamReader(stream); var str = sr.ReadToEnd(); GlobalCommon.Logger.WriteLog(LoggerLevel.DEBUG, $"request content:{str}"); if (!string.IsNullOrEmpty(str)) { foreach (var s in str.Split('&')) { var items = s.Split('='); wp[DomainKey.POST_DATA, items[0]] = ComFunc.UrlDecode(items[1]); } } wp.RequestContent = str; } } } }
protected override bool IsValid4Invoke(WebParameter p, GoData d) { //企业微信的来源校验不同 return(true); }
protected override void ProcessRequestInfoWeixin(WebParameter p, GoData d) { //微信相关信息 p.ExtentionObj.weixin = FrameDLRObject.CreateInstance(FrameDLRFlags.SensitiveCase); // 微信企业号的加密签名为msg_signature而不是signature,所以得重写 p.ExtentionObj.weixin.signature = ComFunc.nvl(p[DomainKey.QUERY_STRING, "msg_signature"]); p.ExtentionObj.weixin.timestamp = ComFunc.nvl(p[DomainKey.QUERY_STRING, "timestamp"]); p.ExtentionObj.weixin.nonce = ComFunc.nvl(p[DomainKey.QUERY_STRING, "nonce"]); p.ExtentionObj.weixin.token = ComFunc.nvl(p[DomainKey.CONFIG, "weixin_token"]); p.ExtentionObj.weixin.encrypt_type = ComFunc.nvl(p[DomainKey.QUERY_STRING, "encrypt_type"]) == "" ? "aes" : ComFunc.nvl(p[DomainKey.QUERY_STRING, "encrypt_type"]); p.ExtentionObj.weixin.encrypt_key = ComFunc.nvl(p[DomainKey.CONFIG, "weixin_encry_key"]); p.ExtentionObj.weixin.appid = ComFunc.nvl(p[DomainKey.CONFIG, "weixin_Appid"]); p.ExtentionObj.weixin.appsecret = ComFunc.nvl(p[DomainKey.CONFIG, "weixin_Appsecret"]); p.ExtentionObj.weixin.weixin_mch_ssl_path = ComFunc.nvl(p[DomainKey.CONFIG, "weixin_Mch_SSL_Path"]); p.ExtentionObj.weixin.weixin_mch_ssl_pass = ComFunc.nvl(p[DomainKey.CONFIG, "weixin_Mch_SSL_Pass"]); // 20171124 Ge.Song 针对企业微信添加AgentId p.ExtentionObj.weixin.agentid = ComFunc.nvl(p[DomainKey.CONFIG, "weixin_AgentId"]); p.SetValue("logkey", DateTime.Now.ToString("yyyyMMddHHmmssfff")); string content = string.Empty; if (CurrentContext.Request.Method.ToLower() == "post") { content = p.RequestContent; WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(p.ExtentionObj.weixin.token, p.ExtentionObj.weixin.encrypt_key, p.ExtentionObj.weixin.appid); string msg = ""; wxcpt.DecryptMsg(p.ExtentionObj.weixin.signature, p.ExtentionObj.weixin.timestamp, p.ExtentionObj.weixin.nonce, content, ref msg); content = msg; if (content != "") { XmlDocument doc = new XmlDocument(); //防范xxe攻击 doc.XmlResolver = null; doc.LoadXml(content); var root = doc.FirstChild; foreach (XmlNode node in root.ChildNodes) { // 一般来说企业微信事件只会回传AgentID,ToUserName,Encrypt p[DomainKey.POST_DATA, node.Name] = node.Name == "CreateTime" ? (object)new DateTime(1970, 1, 1).AddSeconds(int.Parse(node.InnerText)) : node.InnerText; if (node.HasChildNodes) { foreach (XmlNode sub in node.ChildNodes) { if (node.Name == "CreateTime") { p[DomainKey.POST_DATA, sub.Name] = new DateTime(1970, 1, 1).AddSeconds(int.Parse(sub.InnerText)); } else { p[DomainKey.POST_DATA, sub.Name] = sub.InnerText; } } } } } //事件触发时的action处理 if (ComFunc.nvl(p[DomainKey.POST_DATA, "MsgType"]) == "event") { p.Action = "event_" + ComFunc.nvl(p[DomainKey.POST_DATA, "Event"]); } else { //普通消息处理,action为消息类型 p.Action = "msg_" + ComFunc.nvl(p[DomainKey.POST_DATA, "MsgType"]); } } else { //action为api_valid的时候为微信服务器的验证请求 p.Action = "api_valid"; } }
protected override void ProcessRequestInfo(WebParameter p, GoData d) { base.ProcessRequestInfo(p, d); ProcessRequestInfoWeixin(p, d); }
protected virtual void ProcessRequestInfoWeixin(WebParameter p, GoData d) { //微信相关信息 p.ExtentionObj.weixin = FrameDLRObject.CreateInstance(FrameDLRFlags.SensitiveCase); p.ExtentionObj.weixin.signature = ComFunc.nvl(p[DomainKey.QUERY_STRING, "signature"]); p.ExtentionObj.weixin.timestamp = ComFunc.nvl(p[DomainKey.QUERY_STRING, "timestamp"]); p.ExtentionObj.weixin.nonce = ComFunc.nvl(p[DomainKey.QUERY_STRING, "nonce"]); p.ExtentionObj.weixin.token = ComFunc.nvl(p[DomainKey.CONFIG, "weixin_token"]); p.ExtentionObj.weixin.encrypt_type = ComFunc.nvl(p[DomainKey.QUERY_STRING, "encrypt_type"]); p.ExtentionObj.weixin.encrypt_key = ComFunc.nvl(p[DomainKey.CONFIG, "weixin_encry_key"]); p.ExtentionObj.weixin.appid = ComFunc.nvl(p[DomainKey.CONFIG, "weixin_Appid"]); p.ExtentionObj.weixin.appsecret = ComFunc.nvl(p[DomainKey.CONFIG, "weixin_Appsecret"]); p.ExtentionObj.weixin.weixin_mch_ssl_path = ComFunc.nvl(p[DomainKey.CONFIG, "weixin_Mch_SSL_Path"]); p.ExtentionObj.weixin.weixin_mch_ssl_pass = ComFunc.nvl(p[DomainKey.CONFIG, "weixin_Mch_SSL_Pass"]); p.SetValue("logkey", DateTime.Now.ToString("yyyyMMddHHmmssfff")); string content = string.Empty; if (CurrentContext.Request.Method.ToLower() == "post") { content = p.RequestContent; //如果内容为aes加密 if (p.ExtentionObj.weixin.encrypt_type == "aes") { WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(p.ExtentionObj.weixin.token, p.ExtentionObj.weixin.encrypt_key, p.ExtentionObj.weixin.appid); string msg = ""; var result = wxcpt.DecryptMsg(p.ExtentionObj.weixin.signature, p.ExtentionObj.weixin.timestamp, p.ExtentionObj.weixin.nonce, content, ref msg); content = msg; } var contentobj = FrameDLRObject.IsXmlThen(content, null, FrameDLRFlags.SensitiveCase); if (contentobj != null) { var root = (FrameDLRObject)contentobj.GetValue("xml"); foreach (var item in root.Items) { if (item.Key == "CreateTime") { p[DomainKey.POST_DATA, item.Key] = new DateTime(1970, 1, 1).AddSeconds(IntStd.IsNotIntThen(item.Value)); } else { p[DomainKey.POST_DATA, item.Key] = item.Value; } if (item.Value is FrameDLRObject) { foreach (var sub in ((FrameDLRObject)item.Value).Items) { if (sub.Key == "CreateTime") { p[DomainKey.POST_DATA, sub.Key] = new DateTime(1970, 1, 1).AddSeconds(IntStd.IsNotIntThen(sub.Value)); } else { p[DomainKey.POST_DATA, sub.Key] = sub.Value; } } } } } //微信推送过来的xml对象 p.ExtentionObj.weixin.RecieveXMLObject = contentobj; //事件触发时的action处理 if (ComFunc.nvl(p[DomainKey.POST_DATA, "MsgType"]) == "event") { p.Action = "event_" + ComFunc.nvl(p[DomainKey.POST_DATA, "Event"]); } else { //普通消息处理,action为消息类型 p.Action = "msg_" + ComFunc.nvl(p[DomainKey.POST_DATA, "MsgType"]); } } else { //action为api_valid的时候为微信服务器的验证请求 p.Action = "api_valid"; } }