且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否可以使用Web服务ASP.NET C#从一个应用程序获取会话值到另一个应用程序

更新时间:2022-11-05 17:33:39

您想通过Web方法访问什么会话?应用程序无法共享Session,因此您不太可能将其用于工作。如果用户正在浏览两个站点,那么他有两个会话,但是如果他正在浏览一个站点并且该站点向另一个站点发出呼叫,则它无法访问该站点上的该用户的会话,而是在该站点上创建新的会话站点。

Below is my Web Service code:

[WebMethod(EnableSession = true)]
public string GetStringValue(string sessionstring)
{
// return sessionstring+"1";

 HttpContext.Current.Session["regid"] = sessionstring;

 return HttpContext.Current.Session["regid"].ToString();
}

Below is my first application Code for on button click passing session value to web service

   protected void Button1_Click(object sender, EventArgs e)
{
    localhost.WebService objserv1 = new localhost.WebService();
    Session["RegId"] = "Mazar";
    Label.Text =objserv1.GetStringValue(Session["RegId"].ToString());

}


In second Application need to show on page load.

     protected void Page_Load(object sender, EventArgs e)
{

 //    localhost.WebService objserv1 = new localhost.WebService();
 //     lbl_Value.Text = objserv1.GetStringValue(HttpContext.Current.Session["regid"].ToString());

}


In Second application trying to showing first application session value but not getting any Idea.

<a href="https://i.stack.imgur.com/7gjzi.png">enter image description here

[^]

What I have tried:

Getting first application session value to second application

What "Session" do you want to access via the web method? Applications can't share Session so it's unlikely you're going to get this to work. If a user is browsing both sites then he has two Sessions, but if he is browsing one site and that site makes a call to the other then it can't access that user's Session on that site, but instead creates a new Session on that site.