【问题标题】:session gets cleared once method finish一旦方法完成,会话就会被清除
【发布时间】:2016-10-12 06:11:36
【问题描述】:

我的webservice.cs 页面中有两种方法,如下所示。

[WebMethod(EnableSession = true)]
        public void checkEmployee(string emailId, string password)
        {
            List<Employee> ListEmp = new List<Employee>();
            string cs = ConfigurationManager.ConnectionStrings["rushDB"].ConnectionString;
            using (SqlConnection con = new SqlConnection(cs))
            {
                SqlCommand cmd = new SqlCommand("select * from Employees where Email='" + emailId + "' AND Password='" + password + "'", con);
                string personName = null;
                string personEmail = null;
                string personDeptName = null;
                con.Open();
                SqlDataReader rdr = cmd.ExecuteReader();
                if (rdr.HasRows == true)
                {
                    while (rdr.Read())
                    {
                        Employee empChild = new Employee();

                        empChild.ID = Convert.ToInt16(rdr["ID"]);
                        empChild.Name = rdr["Name"].ToString();
                        empChild.Email = rdr["Email"].ToString();
                        empChild.Password = rdr["Password"].ToString();
                        empChild.DepartName = rdr["DepartName"].ToString();

                        personName = rdr["Name"].ToString();
                        personEmail = rdr["Email"].ToString();
                        personDeptName = rdr["DepartName"].ToString();

                        ListEmp.Add(empChild);
                    }
                    HttpContext.Current.Session["NamePerson"] = personName;
                    HttpContext.Current.Session["EmailPerson"] = personEmail;
                    HttpContext.Current.Session["DepartPerson"] = personDeptName;
                    JavaScriptSerializer js = new JavaScriptSerializer();
                    Context.Response.Write(js.Serialize(ListEmp));
                }
            }
        }

        [WebMethod(EnableSession = true)]
        public void GetCurrentData()
        {
            Employee ListEmp = new Employee();

            if (HttpContext.Current.Session["NamePerson"] != null)
                ListEmp.Name = HttpContext.Current.Session["NamePerson"].ToString();
            if (HttpContext.Current.Session["EmailPerson"] != null)
                ListEmp.Email = HttpContext.Current.Session["EmailPerson"].ToString();
            if (HttpContext.Current.Session["DepartPerson"] != null)
                ListEmp.DepartName = HttpContext.Current.Session["DepartPerson"].ToString();

            JavaScriptSerializer js = new JavaScriptSerializer();
            Context.Response.Write(js.Serialize(ListEmp));
        }

我的问题是在 checkEmployee 中创建的会话在该方法 checkEmployee 完成执行后消失了。

我想在 GetCurrentData 中检索会话值,但我无法这样做。任何解决方案都会帮助我。我用谷歌搜索了它,但没有帮助。

【问题讨论】:

    标签: asp.net web-services session


    【解决方案1】:

    您需要一个 cookie-jar 才能在 Web 服务上保留会话状态。根据您使用服务的方式,如果您之前没有做过,它可能会相当变得复杂......

    请看以下文章:

    http://www.codeproject.com/Articles/322436/RestSessionState

    http://www.codeproject.com/Articles/188749/WCF-Sessions-Brief-Introduction

    【讨论】:

    • 我不知道这是否有用,但是我的问题没有解决
    • @Rush.2707 - 在处理网页时,浏览器通过使用会话 cookie 来帮助保持会话状态。有了网络服务,您就没有那么奢侈了。相反,您需要使用一种方法来登录,该方法返回一个令牌(如 cookie)。现在您有了令牌,您可以在每个后续请求中继续将其发回,以便服务器可以识别您。
    猜你喜欢
    • 1970-01-01
    • 2022-08-05
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    • 2018-02-07
    相关资源
    最近更新 更多