Question

I am having issue setting a cookie to a web-service call in .NET. Before using any of the calls of the wsdl provided, i must provide a cookie that is obtained upon logging in to the clients website. I have a method to login and retrieve the cookie, where I then pass it to my makeSearch method (shown below). As you can see, i'm setting the cookie in the cookieContainer for the wsdl Object; however, when i check the request made by my AdvancedSearch method, i'm noticing in fiddler that no cookie is being sent. The client provided the solution in Java, but am having issue transferring that to .NET.

The following is the solution in Java code: (port is the wsdl object passed in)

private static void setupClient(Object port, final String cookie) throws Exception {
    Client client = ClientProxy.getClient(port); 
    HTTPConduit http = (HTTPConduit) client.getConduit();
    HTTPClientPolicy policy = http.getClient();
    if (policy == null) {
        policy = new HTTPClientPolicy();
        http.setClient(policy);
    }
    policy.setCookie(cookie);
    policy.setAutoRedirect(true);
}

My code is the following:

public AdvancedSearchResult makeSearch(String cookie) {
    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
    AdvancedSearchResult searchResults = new AdvancedSearchResult();
    Cookie cook= new Cookie("NAME", HttpUtility.UrlEncode(cookie));
    searches.CookieContainer = new CookieContainer();
    searches.CookieContainer.Add(newUri(www.test.com),cook);
    searchResults = searches.AdvancedSearch("search params");
    return searchResults;
}

Can anyone list any ideas or solutions?

Was it helpful?

Solution

I just had this same problem and here is how I solved it

var tmWebServices             = new TM_WebServices();
tmWebServices.CookieContainer = new System.Net.CookieContainer();
tmWebServices.Url             = Test_TM.tmWebServices;

var username     = "reader";  
var passwordHash = Test_TM.passwordHash_Reader;
var sessionID    =  tmWebServices.Login(username, passwordHash);

//Note that this is optional if you set these cookies from the server side
var cookie       =  new System.Net.Cookie("Session",sessionID.str());
tmWebServices.CookieContainer.Add(tmWebServices.Url.uri() , cookie); 

var guidanceItemID = "0c85a318-0c32-4417-9d72-7475bb96517e".guid();

var guidanceItemHtml = tmWebServices.GetGuidanceItemHtml(guidanceItemID);

return "{0}".format(guidanceItemHtml);


//using O2.SecurityInnovation.TeamMentor
//O2File:TM_WebServices.cs
//O2File:Test_TM_Config.cs
//O2Ref:System.Web.Services.dll

The key was adding the

tmWebServices.CookieContainer = new System.Net.CookieContainer();

which made the cookies sent from the server to be resent on further requests.

In the example above, if you call the GetGuidanceItemHtml without a valid Session cookie value you will get a security exception (there is a CAS Security demand on the server side)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top