1

On login, i am saving username into Session["username"]. On next page, I am making a web service call. In web service, I want to access this session.

How to do this?

I've tried even set EnableSession to true of webmethod. But, still on executing Session["username"], instance set to null exception is show.

Please help.

1
  • Hi, are the webservices hosted in the same application as the asp.net application? Commented Jun 16, 2011 at 11:17

3 Answers 3

5

You should never do that.

A Web service is a method of communication between two electronic devices over a network. This 2 devices do not share sessions or any other server information besides those expilicitly defined by the web service signature.

You have to remember that any device can access you web service and it this access should not depends on the language the it is using.

As a result, you should add all the information you want to share via in the response you give.

Sign up to request clarification or add additional context in comments.

1 Comment

While I agree that you should not use the session to communicate between the UI and the Web service, there are legitimate reasons to allow a web service to have it's own session state. I have a web service where the caller must call an authenticate method to use. That method returns an id that the caller includes in subsequent requests. By making that id a session identifier, I can allow it to expire after a given time. If I return everything in the response, a hacker could work out how to forge a request to bypass the timeout.
1

Is that web service exist in the same application? How do you perform your login, do you use Membership provider to do that?

Maybe you just can get a username from

HttpContext.Current.User.Identity.Name

the IPrincipal object corresponding with your logged in user?

Comments

0

You can find at http://msdn.microsoft.com/en-us/library/aa480509.aspx

http://www.codeproject.com/articles/35119/Using-Session-State-in-a-Web-Service.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.