0

I have to do forms authentication to use a service, but I can't get cookie. One solution is every time did two requests one to login page and one to resource I want to get. I did the same thing on wpf and console apps and it works well but not in windows phone 8. here is my code.

CookieContainer cookies = new CookieContainer();
            HttpClientHandler handler = new HttpClientHandler();
            handler.CookieContainer = cookies;

            HttpClient client = new HttpClient(handler);
            var content = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string, string>("username", "someusername"),
                new KeyValuePair<string, string>("password", "somepassword")
            });
            var response = await client.PostAsync("mysite.com/login", content);
            string res = await client.GetStringAsync("mysite.com/posts/getposts");

            Uri uri = new Uri("mysite.com/");

            var responseCookies = cookies.GetCookies(uri);
            foreach (Cookie cookie in responseCookies)
            {
                string name = cookie.Name;
                string value = cookie.Value;
            }

2 Answers2

1

Check that the cookie you are trying to read is not an HttpOnly cookie, in which case will not be exposed in the CookieContainer. If that's the situation, this answer should work for you.

Community
  • 1
  • 1
Ajadex
  • 2,319
  • 1
  • 20
  • 23
0

Maybe this will help you. Check your repsone's Cookies property, whether it contains the cookies.

Community
  • 1
  • 1
zimmerrol
  • 4,872
  • 3
  • 22
  • 41