Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

restsharp - no cookie in response object

I have an issue with one of my requests to localhost server.

To authenticate, I need two cookies, one from sendReqForToken() method and one from sendLoginReq(string login, string pass).

In response I get cookie from sendLoginReq, but not from sendReqForToken().

I don't have idea why one request has a cookie second doesn't.

It is interesting that I get correct token(response content is correct) from sendReqForToken() method, but without any cookie in response header.

This is sendReqForToken() method body:

public void sendReqForToken()
{
    string adres = Globals.TOKEN_URL;    
    RestRequest request = new RestRequest(adres, Method.GET);

    var client = new RestClient();
    client.CookieContainer = new CookieContainer();

    client.ExecuteAsync(request, (response) =>
       {
           if (response.StatusCode == HttpStatusCode.OK)
           {
               var tokenValue = JsonConvert.DeserializeObject<Token.RootObject>(response.Content);
               DataManager.Instance.authToken = tokenValue.authenticity_token;

               if (response.Cookies.Count > 0)
               {
                   var cookie = response.Cookies.FirstOrDefault();
                   DataManager.Instance.cookieJar.Add(new Uri(Globals.TOKEN_URL), new Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain));
               }
           }
           else
           {
           }
       }); 
}

response.Cookies.Count always is equal to 0. response.cookies property always is equal to null.

This is sendLoginReq method body:

 public void sendLoginReq(string login, string pass)
{
    login = "admin";
    pass = "admin";

    string adres = Globals.LOGIN_URL;
    RestRequest request = new RestRequest(adres, Method.POST);
    var client = new RestClient();

    request.RequestFormat = DataFormat.Json;
    try
    {
        request.AddBody(new
        {
            authenticity_token = DataManager.Instance.authToken,
            commit = "Login",
            utf8 = true,
            user_session = new
            {
                email = login,
                password = pass
            }
        });
    }
    catch
    {
    }

    client.ExecuteAsync(request, (response) =>
    {
        if (response.StatusCode == HttpStatusCode.OK)
        {
            if (response.StatusCode == HttpStatusCode.OK)
            {
                var cookie = response.Cookies.FirstOrDefault();
                DataManager.Instance.cookieJar.Add(new Uri(Globals.LOGIN_URL), new Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain));
            }
        }
        else
        {
        }
    });
}

In second method I get correct cookie.

Thanks a lot for any ideas.

like image 867
user3163231 Avatar asked Jan 05 '14 19:01

user3163231


2 Answers

Thanks @KarthikNishanth. To make it clear:

client.CookieContainer = new CookieContainer ();
var cookie = client.CookieContainer.GetCookieHeader(new Uri("http://domain_or_subdomain.ext"));

var client is a RestClient

After the client.Execute(request); the GetCookieHeader() will return the desired cookie

like image 146
VeYroN Avatar answered Sep 21 '22 00:09

VeYroN


I had the same problem, your server sends you a cookie with HTTPonly=true parameter, you should change HTTOnly parameter to false and then you can grab the cookie from token response.

see this link answer to your question

like image 39
Robert Avatar answered Sep 18 '22 00:09

Robert