首页 新闻 会员 周边

asp.net mvc 微信客户端 无法保存 session

0
悬赏园豆:50 [已解决问题] 解决于 2018-01-22 12:14
这是微信查询菜单,域名 是正确的, 已经配置成功, xxx是我 代替的

  dic1.Add(new s_button { type = "view", name = "查询", url = "http://oa.xxx.com/Wx/Customer" });

微信平台配置的url验证
     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();

        }
点击菜单 获取到openID,
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);
            }
            
        }
为了方便测试,我已经把数据库用户绑定了openID,已经测试过 openID一定能获取到,用户一定能获取到,也一定能存入 session 和 Cooke,这个已经测试,写入了日志里,有数据存在,
通过openid获取用户
 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");

        }
点击 查询 获取到openID,获取到相应用户信息存入session后,跳转到 customer页面,,但是session和 Cookes 都是 null, 无论是刷新还是再次进入,都是null,
复制代码
    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();
        }
复制代码
访问微信菜单的 url

有人说是跨域问题,不知道做过的人是怎么处理?或者我哪里不正确请指出来,有更好方法的请指教,我的项目本身用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

 

bky57的主页 bky57 | 初学一级 | 园豆:147
提问于:2018-01-21 12:43
< >
分享
最佳答案
0

什么鬼逻辑,微信服务器请求你的api,你怎么设置session?给微信服务器设置一个session?

收获园豆:40
Eric.luo | 小虾三级 |园豆:853 | 2018-01-22 10:43

呃,,,,

bky57 | 园豆:147 (初学一级) | 2018-01-22 12:12
其他回答(3)
0

在微信中访问的是同一个域名吗?

收获园豆:10
dudu | 园豆:30994 (高人七级) | 2018-01-21 14:48

肯定是同个域名啊,二级域名,微信平台 配置的url  是

http://oa.xxx.com/Wx/WeiXin 菜单 查询 是 http://oa.xxx.com/Wx/Customer  我是能获得 openID,也能存,在控制器里 到了Custoemr  之前存 都为null,这中间没有 重置过,你知道会是哪些问题吗

 
支持(0) 反对(0) bky57 | 园豆:147 (初学一级) | 2018-01-21 17:49
0

要重新授权的时候再保存

bky57 | 园豆:147 (初学一级) | 2018-01-22 12:14
0

这明显是微信客户端cookie丢失了,微信开发最好不要用cookie和session,坑得你不要不要的。

Adming | 园豆:119 (初学一级) | 2018-01-23 10:39
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册