For those that didn’t know, my beautiful wife is pregnant with our first child. The due date is the first week of November. Because of this news I had to change jobs to make more money for my family. It was one of the hardest decisions I had to make as I loved working where I was. However, I’m having a blast working at my new job. I have always been a web developer. It was very rare when I needed to do any Windows Forms development.
I’m working on a cool program that I’m not going to tell you about. However, I have played with many cool pieces of .NET that I’ve used before but not in this context.
Enabling Sessions on a web service is very easy:
78 public bool SessionExists()
80 if (Session["AgencyID"] == null)
81 return false;
83 return true;
However, getting the right cookie information to the windows service I’m writing was a bit difficult. Every web service object has the following object: CookieContainer. This object basically eats and stores all cookies returned from any web service call (You don’t even need to pass anything back, it’s all automatic).
Here is how to use the CookieContainer object. This is a method from my windows service:
81 private bool Authenticate()
87 Authentication.Manager m = new Authentication.Manager(settings.AuthenticationMethod);
88 //Load a Cookie that was stored in an XML file from the Login Factory
89 System.Net.Cookie cookie = m.loadAuthCookie();
90 if (cookie == null) return false;
93 Updater.Updater u = new Updater.Updater();
94 u.CookieContainer = cc;
95 if (u.SessionExists())
96 return true;
98 return false;
100 catch (Exception ex)
102 throw new Exception("Failed to Load Authentication Cookie\n" + ex.Message);
The CookieContainer object must be instantiated before you make your web service call. Once you make your web service call and it completes, you will receive a session cookie. Here is the cookie in XML form (this is the format I use):
1 <?xml version="1.0" encoding="utf-8"?>