473,396 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

123 100+
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
1 5264
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

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

Similar topics

0
by: David | last post by:
So I'm a new user to Visual Studio .NET. I'm having a problem with an initial setup of a Windows Form Application for a Visual C++ Project. The Windows Form Application (.NET) template is not...
0
by: forge | last post by:
Installing Merge Modules from C# Windows Form Application (not using a setup and deployment project) Hi, Currently, we have an InstallShield project that creates a setup.exe that only...
1
by: Dean R. Henderson | last post by:
I have a Windows library that I bind into a Windows Form application and into an ASP.NET Web Application. I have a procedure in the library that executes a DTS Package. I have also used...
4
by: bob lambert | last post by:
Help I am trying to deploy to another pc a vb.net std 2002 windows form application. I am confused. I created a project - windows form I built form, compiled and debugged. I created a...
3
by: Henry Seymour | last post by:
I've developed a .Net Windows Form application and used the Click-Once Deployment. In that application I would like to allow the user a click-once method of uninstalling it. I do not find an...
1
by: Dave | last post by:
Hello all, I have an application that is accessing files from a mapped drive. I have full access to the share that is mapped and from explorer can delete files as I need. My application needs to...
0
by: querry | last post by:
Hi all, I am developing a Windows form Application in VC++.NET using the Visual Studio 2005 installed on Windows Server 2003. The UI part of the project is almost complete. I am now looking forward...
1
by: Prats | last post by:
I have a windows form application developed in C++/CLR How can I verify that only one instance of the application is running on the user system at any given time. Thanks
2
by: ashkan0021 | last post by:
hi, i have a solution in C#.Net that includes 2 project and ASP.net & A Windows form application project, i want to open some pages of my asp.net project in my windows application, i'd be realy...
5
by: Henrik Söderber | last post by:
Hi! I use C# 4.0 to build a windows form application. I have a Windows Forms Timer that should tick every second. In every tick I do some work (get GPS position, update a map, save to a local DB...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.