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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • perhapscwk
    New Member
    • Sep 2007
    • 123

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

    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_KE Y 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_KE Y.

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

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


    Code:
    private void button1_Click(object sender, EventArgs e)
            {
    try
                {
                    Uri uri = new Uri("http://localhost:8080/qcbin/authentication-point/authenticate");
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
                    request.Method = "POST";
                    NetworkCredential credentials = new NetworkCredential("user", "password");
                    request.Credentials = credentials;
    
                    WebResponse v = request.GetResponse();
                    Stream rStream = v.GetResponseStream();
                    StreamReader str = new StreamReader(rStream);
                    if (str.EndOfStream != true)
                    {
                        richTextBox1.Text = str.ReadToEnd();
    
                    }
                    v.Close();
                    rStream.Close();
                    str.Close();
                    richTextBox1.Text = "success";
    
                }
    
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK);
                }     
            }
    button 2
    Code:
    private void button2_Click_1(object sender, EventArgs e)
            {
    
                HttpWebRequest request
                    = WebRequest.Create("http://localhost:8080/qcbin/rest/domains/DEFAULT/projects/demoproject/defects") as HttpWebRequest;
    
                // Get response  
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    // Get the response stream  
                    StreamReader reader = new StreamReader(response.GetResponseStream());
    
                    // output  
                     richTextBox1.Text = reader.ReadToEnd();
                }  
                
                
            }
    Last edited by perhapscwk; Feb 21 '12, 11:10 PM. Reason: add more info
  • ddikman
    New Member
    • Oct 2014
    • 1

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

    Code:
    private void Authenticate()
    {
    	var request = WebRequest.Create(_connectionInfo.Url + "/authentication-point/authenticate");
    	var auth = Encoding.UTF8.GetBytes(_connectionInfo.Username + ":" + _connectionInfo.Password);
    	request.Headers[HttpRequestHeader.Authorization] = "Basic " + Convert.ToBase64String(auth);
    
    	var response = request.GetResponse();
    	_cookie = response.Headers["Set-Cookie"];
    }
    
    private XElement GetXml(string urlPart)
    {
    	var url = _connectionInfo.Url + string.Format("/rest/domains/{0}/projects/{1}/{2}", _connectionInfo.Domain, _connectionInfo.Project, urlPart);
    	using (var client = new WebClient())
    	{
    		client.Headers[HttpRequestHeader.Cookie] += ";" + _cookie;
    		var response = client.DownloadString(url);
    		return XDocument.Parse(response).Root;
    	}
    }

    Comment

    Working...