Saving Cookies from RestResponse or Passing Session IDs - URGENT HELP REQUIRED

6,739 views
Skip to first unread message

Devesh Tripathi

unread,
Jul 24, 2012, 9:45:38 AM7/24/12
to rest...@googlegroups.com
Hello,

If anyone could help me out with my issue, i would be really thankful.

I have written a c# code using RestSharp library to interact with RightScale API.

The code works fine with one set of username and password, but when i replace the username and password with a new one, i get the response "Basic auth is deprecated for calls other than login. Please 'login' to get a session and pass the session back for further actions."

Can anyone guide me in the right direction. I find it really weird that the code works for one set of credentials only and not with any other username and password.

How do i save cookies and pass them as reference in the next restrequest?

Thank you.
Devesh 

Peter Johanson

unread,
Jul 24, 2012, 1:51:00 PM7/24/12
to rest...@googlegroups.com
Devesh,

Try setting the IRestClient.CookieContainer property to a new instance
of a System.Net.CookieContainer. That should let the cookies be reused
for all requests made by that IRestClient instance.

-pete
--

Devesh Tripathi

unread,
Jul 24, 2012, 1:54:21 PM7/24/12
to rest...@googlegroups.com
Thanks for your response Pete. I am using the following code. In the last section, i am getting the session id from the cookie into c.value.
Can you help me by letting me know more about usage of cookiecontainer class. I tried to find detailed documentation or example about it, but couldn't. I am in deep trouble and if you could help me with this, i would really appreciate it.


            _accountNumber = accountNumber;
            _username = username;
            _password = password;

            _baseUrl = "https://my.rightscale.com/api/acct/" + _accountNumber + "/";

            var request = new RestRequest();
            request.Method = Method.GET;
            request.Resource = "login";

            var client = new RestClient();
            client.BaseUrl = _baseUrl;
            client.Authenticator = new HttpBasicAuthenticator(_username, _password);

            var myparm = new RestSharp.Parameter { Name = "X-API-VERSION", Value = "1.0", Type = ParameterType.HttpHeader };
            request.AddParameter(myparm);

            var response = client.Execute(request);

            foreach (var c in response.Cookies)
            {
                Console.WriteLine(c.Name);
                Console.WriteLine(c.Value);
                if (c.Name.Contains("_session_id") == true)
                {
                    _cookie = c.Value.ToString();

Peter Johanson

unread,
Jul 24, 2012, 2:12:57 PM7/24/12
to rest...@googlegroups.com
Devesh,

I can't check this, since I'm in the airport right now, but something
like:

var client = new RestClient("https://my.rightscale.com/api/acct/" +
account) {
CookieContainer = new CookieContainer ()
};

And then use that same client instance for all your calls.

-pete
--

Reply all
Reply to author
Forward
0 new messages