2016-10-12 7 views
0

私のwebservice.csページには以下の2つの方法があります。セッションが完了するとセッションがクリアされます

[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でセッション値を取得したいが、これを行うことができない。どのような解決策が私を助けるだろう。私はそれをgoogledしたが、それは助けにはなりませんでした。

答えて

1

ウェブサービスでセッション状態を保持できるようにするには、クッキージャーが必要です。

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

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

:あなたはあなたが前にそれを行っていない場合、それは はかなり複雑...

は、以下の記事をご覧に持って取得することができ、サービスを消費しているかに応じて、

+0

これは参考になるかどうかわかりませんが、私の問題は解決しませんでした。 –

+0

@ Rush.2707 - ウェブページを扱うとき、ブラウザはセッションCookieを使用してセッション状態を保持するのに役立ちます。 Webサービスでは、その贅沢はありません。代わりに、トークン(クッキーのような)を返すログインメソッドを使用する必要があります。今度は、トークンを持っているので、サーバーがあなたを識別できるように、後続の要求ごとにトークンを送り返すことができます。 – Ted

関連する問題