Use session and cookie in webservice

Source: Internet
Author: User
To use session in webservice, 1. you must add the [WebMethod (EnableSession = true)] modifier to the server method. on the client, a new CookieContainer () must be instantiated for its CookieContainer after the webservice proxy class is instantiated to use the session storage state. 3. in addition, as long as multiple webservice proxies contain the same cookie, the same session can be shared. The cookie is used by CookieContainer of the proxy class. getCookies (new Uri (s. url) ["ASP. NET_SessionId "]. If other webserivce proxy classes need to use the same session, CookieContainer can be used. add method to Add the obtained cookie. If you only want to use cookies, you only need to set them on the client. The server does not need to add Attribute Modification like session.
In addition, webmethod is only used for the public Member method. It is not used for static purposes. Although no error is reported, the static method cannot be found in the client proxy class.

When using asp.net to write webservice, session is not supported by default, but we canEnableSessionSet the option to true to enable it explicitly. See the following example:

1. Create a WebSite 

2. Create a web Service WebService. asmx. It has the following two methods:

[WebMethod (EnableSession = true)]
Public string Login (string name)
{
Context. Session ["name"] = name;
Return name;
}

[WebMethod (EnableSession = true)]
Public string GetName ()
{
If (Context. Session ["name"]! = Null)
Return Context. Session ["name"]. ToString ();
Else
Return "";
}

3 add asp.net page SessionInWebservice. aspx

<Form id = "form1" runat = "server">
<Div>
<Asp: TextBox ID = "txtName" runat = "server"> </asp: TextBox>
<Asp: Button ID = "btnLogin" runat = "server"
Text = "Login" OnClick = "btnLogin_Click"/>
</Div>
<Div>
<Asp: Button ID = "btnGetName" runat = "server"
Text = "GetName" OnClick = "btnGetName_Click"/>
<Asp: Label ID = "lblName" runat = "server" Text = "Label"> </asp: Label>
</Div>
</Form>

SessionInWebservice. aspx. cs

Protected void btnLogin_Click (object sender, EventArgs e)
{
WebService ws = new WebService ();

Ws. Login (txtName. Text );
}
Protected void btnGetName_Click (object sender, EventArgs e)
{
WebService ws = new WebService ();
LblName. Text = ws. GetName ();
}

The problem seems to have ended. After logging the user name by pressing the Login button, you can press GetName to get the name you just entered.

However, if we create another website and add a web reference to call the webservice we just compiled, the problem arises, the GeName method does not get the username we just logged on to (if you call this method in winform, the same problem will occur ). Is this method not feasible?

Otherwise, we can assign a value to the CookieContainer of the WebService.SessionInWebservice. aspx. csCode:

Private static System. Net. CookieContainer cookieContainer
= New System. Net. CookieContainer ();

Protected void btnLogin_Click (object sender, EventArgs e)
{
Localhost. WebService ws = new localhost. WebService ();
Ws. CookieContainer = cookieContainer;
Ws. Login (txtName. Text );
}
Protected void btnGetName_Click (object sender, EventArgs e)
{
Localhost. WebService ws = new localhost. WebService ();
Ws. CookieContainer = cookieContainer;
LblName. Text = ws. GetName ();
}

Note: LoginMethod andGetNameThe method must specify the same CookieContainer, so we use static variables here.

However, if the webservice is called on different pages, the problem persists. Therefore, we need to modify the code again and inherit the webservice by writing a new class, assign a value to CookieContainer to solve the problem: 

Public class WebService1: localhost. WebService
{
Private static System. Net. CookieContainer cookieContainer;

Static WebService1 ()
{
CookieContainer = new System. Net. CookieContainer ();
}

Public WebService1 ()
{
This. CookieContainer = cookieContainer;
}
}

You do not need to assign a new value to CookieContainer during the call:

Protected void btnLogin_Click (object sender, EventArgs e)
{
WebService1 ws = new WebService1 ();
Ws. Login (txtName. Text );
}
Protected void btnGetName_Click (object sender, EventArgs e)
{
WebService1 ws = new WebService1 ();
LblName. Text = ws. GetName ();
}

Ps: in actual use, if the two pages that call webservice are in the same website, the same session cannot be used for the two pages of different websites.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.