8

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?

bluish
  • 26,356
  • 27
  • 122
  • 180
user1548923
  • 81
  • 2
  • 5
  • That should be RESTful because it works for one user account meaning i can query using RightScale API for one user account. But if change only the username and password variables in the code, it doesnt allow me to go past the login thing. Probably i need to save cookies but i am not sure how to do that. – user1548923 Jul 24 '12 at 14:35
  • 3
    Sorry, if you need to save a session cookie, it's by definition not RESTful. But anyway, take a look at https://github.com/restsharp/RestSharp/wiki/Cookies – cHao Jul 24 '12 at 14:55
  • How to use cookiecontainer in this scenario so that it gets saved and i can use it for subsequent requests please. var request = new RestRequest(); 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); – user1548923 Jul 24 '12 at 15:12
  • I'm not deciphering all that. Edit it into the question, where it can be formatted appropriately. – cHao Jul 25 '12 at 19:58

1 Answers1

8

RestSharp 102.4+ supports using a shared System.Net.CookieContainer for all requests from the same IRestClient. By doing so, any cookies set or unset in responses will be used in subsequent requests. In order to use a shared CookieContainer, simply set the property on your RestClient instance before using it:

var client = new RestClient("http://server/");
client.CookieContainer = new System.Net.CookieContainer();

Source: https://github.com/restsharp/RestSharp/wiki/Cookies

bluish
  • 26,356
  • 27
  • 122
  • 180
Marcos
  • 3,263
  • 1
  • 23
  • 22