Session key C#

Added by Runar over 8 years ago

I have successfully managed to log on to the REST service thanks to the C# login example. Now I'm trying to use the other methods in the API.

If I understand correctly you have to use the session key in all the requests, but I don't know how to set that up. Below is my code for loging out, but it does not work, (401) Unauthorized. What am I missing out here?

Uri address = new Uri(BASE_URL + "/" + VERSION + "/" + "login" + "/" + session_key);
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
request.Method = "DELETE";
request.Accept = "application/xml";
request.ContentType = "application/x-www-form-urlencoded";
request.PreAuthenticate = true;
request.Credentials = new NetworkCredential(session_key, session_key);


Replies (3)

RE: Session key C# - Added by Nordnet Simon over 8 years ago

Hi, that is a mystery - I ran into the same problem - my impression is that it should work like that. I ended up adding the "Authorization" header by hand.

Try this:

byte[] authBytes = Encoding.UTF8.GetBytes((session_key + ":" + session_key).ToCharArray());
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(authBytes);

Hopefully some C# expert can tell us what the problem is.

RE: Session key C# - Added by Runar over 8 years ago

Got it to work with your solution=) Thanks for quick response!

RE: Session key C# - Added by Henrik over 8 years ago

Yes, to make a long story very short, that is the 'correct' way to do it. Manually add the header.