sessioncookiesxbap

Cannot make XBAP cookies work


I am trying to make a XBAP application communicating with a webservice with login. But I want the user to skip the login step if they already logged in the last seven days. I got it to work using html/aspx. But it fails continuously with XBAP.

While debugging, the application is given full trust.

This is the code I have so far to write the cookie:

    protected static void WriteToCookie(
        string pName,
        Dictionary<string, string> pData,
        int pExiresInDays)
    {
        // Set the cookie value.
        string data = "";
        foreach (string key in pData.Keys)
        {
            data += String.Format("{0}={1};", key, pData[key]);
        }
        string expires = "expires=" + DateTime.Now.AddDays(pExiresInDays).ToUniversalTime().ToString("r");
        data += expires;

        try
        {
            Application.SetCookie(new Uri(pName), data);
        }
        catch (Exception ex)
        {
        }
    }

And this is what I have to read the cookie:

    protected static Dictionary<string, string> ReadFromCookie(
        string pName)
    {
        Dictionary<string, string> data = new Dictionary<string, string>();
        try
        {
            string myCookie = Application.GetCookie(new Uri(pName));

            // Returns the cookie information.
            if (String.IsNullOrEmpty(myCookie) == false)
            {
                string[] splitted = myCookie.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                string[] sub;
                foreach(string split in splitted)
                {
                    sub = split.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    if (sub[0] == "expires")
                    {
                        continue;
                    }
                    data.Add(sub[0], sub[1]);
                }
            }
        }
        catch(Exception ex)
        {

        }
        return data;
    }

The pName is set with:

string uri = "http://MyWebSiteName.com";

When the user authenticate the first time, I call the WriteToCookie function and set it with 7 days to expire. It looks like everything is fine as I get no exception of error messages. (I have a break point in the catch)

After that, I close the session and start it again. The first thing I do is a ReadFromCookie. Then I get an exception with the following message: No more data is available So my application is sending the user automatically back to the login screen.

I also tried to do a ReadFromCookie right after the WriteToCookie in the same session, and I get the same error.

Application.SetCookie(new Uri("http://MyWebSiteName.com/WpfBrowserApplication1.xbap"), "Hellllo");
string myCookie2 = Application.GetCookie(new Uri("http://MyWebSiteName.com/WpfBrowserApplication1.xbap"));

It seems to me that the cookie is not even written in the first place. So I am guessing I am doing something wrong. Maybe the uri I am using is wrong. Is there a specific format needed for it? Just like you need a very specific format for the expire date.

I have been searching quite a lot of internet for a good sample/tutorial about using cookies with XBAP, and I could not find anything really well documented or tested. A lot of people say that it works, but no real sample to try. A lot of people also handle the authentication in html, then go to the XBAP after successfully reading/writing the cookies. I would prefer a full XBAP solution if possible.

To answer some questions before they are asked, here are the project settings:

I also created a certificate, and added it the 3 stores like explained in "publisher cannot be verified" message displayed So I do not have the warning popup anymore. I just wanted to make sure that it was not a permission issue.


Solution

  • Finally found the answer to this problem.

    Thanks for this CodeProject I was finally able to write/read cookies from the XBAP code. As I had guessed, the URI needs to be very specific and you cannot pass everything you want in it.

    What did the trick was using: BrowserInteropHelper.Source

    In the end the read/write code looks like:

    Application.SetCookie(BrowserInteropHelper.Source, data);
    
    string myCookie = Application.GetCookie(BrowserInteropHelper.Source);
    

    It looks like you cannot use ';' to separate your own data. If you do so, you will only get the first entry in your data. Use a different separator (ex: ':') and then you can get everything back The data look like this:

    n=something:k=somethingElse;expires=Tue, 12 May 2015 14:18:56 GMT ;

    The only thing I do not get back from Application.GetCookie is the expire date. Not sure if it is normal or not. Maybe it is flushed out automatically for some reason. If someone knows why, I would appreciate a comment to enlighten me.

    At least now I can read/write data to the cookie in XBAP. Yeah!