By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,617 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

C# .NET windows form application (store rest API authenticate)?

100+
P: 123
i use below to authenticate a application thru REST API
(click button1).
It success, however, after that, I click button2 and
want to get data thru button 2, but it still return me 401 but I have authenticate thru button 1 already.
how to store the authenticate to C# .net (windows form application)?

below is info from the rest api manual,
Client sends a valid Basic Authentication header to the authentication point. BTW I can get the LWSSO_COOKIE_KEY but how to use it for another web request to access other data

GET /qcbin/authentication-point/authenticate
Authorization: Basic ABCDE123

Server validates the Basic authentication headers, creates a new LW-SSO token and returns it as LWSSO_COOKIE_KEY.

HTTP/1.1 200 OK
Set-Cookie: LWSSO_COOKIE_KEY={cookie}

he application can now access data and services using the token. At the end of the session, log off to discard the token.


Expand|Select|Wrap|Line Numbers
  1. private void button1_Click(object sender, EventArgs e)
  2.         {
  3. try
  4.             {
  5.                 Uri uri = new Uri("http://localhost:8080/qcbin/authentication-point/authenticate");
  6.                 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
  7.                 request.Method = "POST";
  8.                 NetworkCredential credentials = new NetworkCredential("user", "password");
  9.                 request.Credentials = credentials;
  10.  
  11.                 WebResponse v = request.GetResponse();
  12.                 Stream rStream = v.GetResponseStream();
  13.                 StreamReader str = new StreamReader(rStream);
  14.                 if (str.EndOfStream != true)
  15.                 {
  16.                     richTextBox1.Text = str.ReadToEnd();
  17.  
  18.                 }
  19.                 v.Close();
  20.                 rStream.Close();
  21.                 str.Close();
  22.                 richTextBox1.Text = "success";
  23.  
  24.             }
  25.  
  26.             catch (Exception ex)
  27.             {
  28.                 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK);
  29.             }     
  30.         }
  31.  
  32.  
button 2
Expand|Select|Wrap|Line Numbers
  1. private void button2_Click_1(object sender, EventArgs e)
  2.         {
  3.  
  4.             HttpWebRequest request
  5.                 = WebRequest.Create("http://localhost:8080/qcbin/rest/domains/DEFAULT/projects/demoproject/defects") as HttpWebRequest;
  6.  
  7.             // Get response  
  8.             using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
  9.             {
  10.                 // Get the response stream  
  11.                 StreamReader reader = new StreamReader(response.GetResponseStream());
  12.  
  13.                 // output  
  14.                  richTextBox1.Text = reader.ReadToEnd();
  15.             }  
  16.  
  17.  
  18.         }
  19.  
  20.  
Feb 21 '12 #1
Share this Question
Share on Google+
1 Reply


P: 1
I know this is old but in case someone comes across it again, I used these simple snippets:

Expand|Select|Wrap|Line Numbers
  1. private void Authenticate()
  2. {
  3.     var request = WebRequest.Create(_connectionInfo.Url + "/authentication-point/authenticate");
  4.     var auth = Encoding.UTF8.GetBytes(_connectionInfo.Username + ":" + _connectionInfo.Password);
  5.     request.Headers[HttpRequestHeader.Authorization] = "Basic " + Convert.ToBase64String(auth);
  6.  
  7.     var response = request.GetResponse();
  8.     _cookie = response.Headers["Set-Cookie"];
  9. }
  10.  
  11. private XElement GetXml(string urlPart)
  12. {
  13.     var url = _connectionInfo.Url + string.Format("/rest/domains/{0}/projects/{1}/{2}", _connectionInfo.Domain, _connectionInfo.Project, urlPart);
  14.     using (var client = new WebClient())
  15.     {
  16.         client.Headers[HttpRequestHeader.Cookie] += ";" + _cookie;
  17.         var response = client.DownloadString(url);
  18.         return XDocument.Parse(response).Root;
  19.     }
  20. }
Oct 21 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.