dic1.Add(new s_button { type = "view", name = "查询", url = "http://oa.xxx.com/Wx/Customer" });
public ActionResult WeiXin() { //LogHelper.WriteLog("请求的方式:" + System.Web.HttpContext.Current.Request.HttpMethod + " ;请求的url:" + System.Web.HttpContext.Current.Request.Url.ToString()); if (System.Web.HttpContext.Current.Request.HttpMethod == "GET") { string rechostr = GetSignature(); System.Web.HttpContext.Current.Response.Write(rechostr);//发送回去 System.Web.HttpContext.Current.Response.End(); } else { AjaxWeixinAcceptMsg(); } return View(); }
public void AjaxWeixinAcceptMsg() { try { int ret = 0; //请求发过来的是密文数据 string postData = ""; using (Stream sr = System.Web.HttpContext.Current.Request.InputStream) { Byte[] bytestr = new byte[sr.Length]; sr.Read(bytestr, 0, (int)sr.Length); postData = Encoding.UTF8.GetString(bytestr); XmlDocument doc = new XmlDocument(); doc.LoadXml(postData); XmlNode root = doc.FirstChild; string toUserName = root["ToUserName"].InnerText;//应用 string fromUserName = root["FromUserName"].InnerText;//用户 openid string msgType = root["MsgType"].InnerText; //授权同意后需要跳转的页面 if (msgType == "event") { //设置session SetSession(fromUserName); } } } catch (Exception ex) { LogHelper.WriteLog("事件解析失败:" + ex.Message); } }
public void SetSession(string openID) { session_Admin.WxID = openID; LogHelper.WriteLog("设置前的cookies" + Request.Cookies["user"].ToJSON()); LogHelper.WriteLog("设置前的session:" + System.Web.HttpContext.Current.Session["S_User"].ToJSON()); if (openID != null) { cook_user = new HttpCookie("user"); var admin = _adminService.GetAdmins().Where(a => a.WxID == openID).SingleOrDefault(); if (admin == null) admin = _adminService.GetAdmins().Where(a => a.AdminID == "01234").SingleOrDefault(); if (admin != null) { cook_user.Values.Add("userID", admin.AdminID); cook_user.Values.Set("userName", admin.AdminName); cook_user.Values.Set("OpenID", openID); cook_user.Expires = DateTime.Now.AddMinutes(10); Response.SetCookie(cook_user); Response.AddHeader("P3P", "CP=CAO PSA OUR"); //session System.Web.HttpContext.Current.Session["S_User"] = admin; LogHelper.WriteLog("已经设置cookies:" + Request.Cookies["user"].ToJSON()); LogHelper.WriteLog("已经设置session:" + System.Web.HttpContext.Current.Session["S_User"].ToJSON()); } } else LogHelper.WriteLog("未找到openID"); }
public ActionResult Customer() { var admin = System.Web.HttpContext.Current.Session["S_User"]; LogHelper.WriteLog("当前保存的session:" + System.Web.HttpContext.Current.Session["S_User"].ToJSON()); LogHelper.WriteLog("当前保存的cookies:" + Request.Cookies["user"].ToJSON()); return View(); }
有人说是跨域问题,不知道做过的人是怎么处理?或者我哪里不正确请指出来,有更好方法的请指教,我的项目本身用session 是没有其他任何问题,单单是加入了 微信 这个功能,唯独 在 微信里访问 设置的session 在其他的任何页面都无法获取
下面是 日志 已经反复测试过,能获取到openID,能存入session 和cookie,该日志我为了测试 看的更清晰 整理的,没有截完整
2018/1/21 12:25:29 ============================================================== 设置前的cookiesnull 2018/1/21 12:25:29 ============================================================== 设置前的session:null 2018/1/21 12:25:29 ============================================================== 已经设置cookies:{"Name":"user","Path":"/","Secure":false,"HttpOnly":false,"Domain":null,"Expires":"\/Date(1516509329930)\/","Value":"userID=01234&userName=张三&OpenID=o6qkk45fdfhslBXz5XnuQ","HasKeys":true,"Values":["userID","userName","OpenID"]} 2018/1/21 12:25:29 ============================================================== 已经设置session:{"AdminID":"01234","AdminPwd":"c0575424654b40e483ad9187da1","AdminName":"张三“ 2018/1/21 12:25:30 ============================================================== 当前保存的session:null 2018/1/21 12:25:30 ============================================================== 当前保存的cookies:null
什么鬼逻辑,微信服务器请求你的api,你怎么设置session?给微信服务器设置一个session?
呃,,,,
在微信中访问的是同一个域名吗?
肯定是同个域名啊,二级域名,微信平台 配置的url 是
http://oa.xxx.com/Wx/WeiXin 菜单 查询 是 http://oa.xxx.com/Wx/Customer 我是能获得 openID,也能存,在控制器里 到了Custoemr 之前存 都为null,这中间没有 重置过,你知道会是哪些问题吗
要重新授权的时候再保存
这明显是微信客户端cookie丢失了,微信开发最好不要用cookie和session,坑得你不要不要的。