views:

51

answers:

2

I am using a web service that requires authentication from .NET (Visual Studio 2010). According to the documentation, you first request a session identifier from the first web service. I can do that with no problem. Then you are supposed to call the second web service for actually performing your query, passing the session identifier in a cookie. Here is my sample code:

AuthenticateService authenticate_service = new AuthenticateService();
string session_identifier = authenticate_service.Authenticate();

SearchService search_service = new SearchService();
search_service.CookieContainer = new CookieContainer();
Cookie cookie = new Cookie("Cookie", "SID=" + session_identifier, null, search_service.Url);
search_service.CookieContainer.Add(cookie);

search_service.Test();

However, I am getting the following exception on the last line:

System.Web.Services.Protocols.SoapException was unhandled Message=Session ID cookie value cannot be null or empty string - It is required that the high level Web service client program participate in the session initialized by the server.

Does anybody know how to properly send a cookie with a session ID to a web service?

A: 

I found this somewhere along the line. It is a cookieawarewebclient that I have been using. This allows me to have the ease ofuse of WebClient and pass cookies.

   public class CookieAwareWebClient : WebClient
    {

        private CookieContainer m_container = new CookieContainer();

        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);
            if (request is HttpWebRequest)
            {
                (request as HttpWebRequest).CookieContainer = m_container;
            }
            return request;
        }
    }

Then you can just use the WebClient methods and the cookie is passed automatically after you authenticate

example code:

 CookieAwareWebClient webClient = new CookieAwareWebClient();    
            NameValueCollection data = new System.Collections.Specialized.NameValueCollection();
            data["user"] = "myusername"; //now holds a user=username map
            byte[] response = webClient.UploadValues("http://localhost:8080/somewebservice/auth/","POST", data); //point to the webservice authentication URI

now you can use webclient.UploadFile or UploadValues or whatever you want assuming that the previous authentication was OK

Derek
I am not sure how exactly to incorporate this into my code. Is it possible to use a WebClient with web services? If so could you give an example?
Craig
Just use CookieAwareWebClient webClient = new CookieAwareWebClient(); and now webClient will be cookie aware. Then you can use webClient.UploadValues pointed at the service with a username map and as long as you keep using that same webClient variable it is associated with that cookie
Derek
A: 

Just figured it out...

It all had to do with the domain parameter of the Cookie constructor. I was passing search_service.Url because I wasn't sure what it was supposed to be. Apparently it should have been something like "search.google.com". When I passed that to the constructor instead, everything started working as expected.

Craig
I know it doesn't help, but related: http://stackoverflow.com/questions/3916554/what-domain-should-i-give-this-cookie. I solved my own problem too (-:
Oren A