how to exchange session or cookie variables between two webmethods in asp.net webservices
I have two webmethod i have used session variable to exchange the username variable between two webmethods but its displaying null in second webmethod ,can we use cookie as alte开发者_如何学编程rnate to session to store and retrieve username
[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public bool SubmitList1(string username )
{
Session["User_Name"] = username;
.........
.......
}
[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public bool addresslisting( string keyword)
{
string username = Context.Session["User_Name"].ToString();
........
.........
}
Disclaimer: I think that a web service that relies on Session state is just plain WRONG since a web service should be stateless. However:
At http://msdn.microsoft.com/en-us/library/aa480509.aspx you can read about how to use ASP.NET Session in a web service:
- Make sure that /configuration/system.web/sessionState in web.config is configured properly to enable session state
- Make sure that uses the web service has a cookie container where the ASP.NET session cookie can be stored. If the client is from a web browser (e.g. ajax call) this usually works out of the box, but if you are building a standalone client, you have to do some more work, see the link above.
All in all: a bad design decision gives you more work than necessary (sorry for rubbing it in).
I think you should redesign you web service so that you either always send username and password in all methods or add a login method that gives the client a token that is sent with each web service request.
精彩评论