It's all about the answers!

Ask a question

Form authentication against REST Services using C# Dot Net.


Akshansh Khare (56178) | asked Mar 15 '12, 8:38 a.m.
Hello Community,

We are building a dot net c# console application that reads data from a REST Web Services.
The REST Web Service uses form authentication to authenticate the user.
But when we made the c# code this does not authenticate the server and give error Time out Error in the Response.

C# Code (Time Out Error)

//Form authentication for REST Services

HttpWebRequest RequestObject = (HttpWebRequest)WebRequest.Create(new Uri(String.Format(serverURI +"/j_security_check?username={0}&password={1}", login, password)));
RequestObject.Method = WebRequestMethods.Http.Post;
RequestObject.ContentType = "application/x-www-form-urlencoded";
RequestObject.Referer = clientUrl;
HttpWebResponse ResponseObject = (HttpWebResponse)RequestObject.GetResponse();


We have also tried to authenticate the server using FORM Authentication with CookieContainer.
But still it gives Time Out Error in the response.

C# Code with Cookie Container Used (Time Out Error)


//Attempt to authenticate with the server with form and cookie
CookieContainer newCookieContainerObject = new CookieContainer();
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(string.Format(serverURI + "/j_security_check?j_username={0}&j_password={1}", UserID, Password));
Request.CookieContainer = newCookieContainerObject;
Request.Method = WebRequestMethods.Http.Post;
Request.ContentType = "application/x-www-form-urlencoded";
HttpWebResponse ResponseObject = (HttpWebResponse)Request.GetResponse();



We have the java code that authenticates the users and we are able to extract the data from the REST Web Services.

Java Code (Works Fine)

// The server requires an authentication: Create the login form

HttpPost formPost = new HttpPost(serverURI+"/j_security_check");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("username", login));
nvps.add(new BasicNameValuePair("password", password));
formPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

// Step (2): The client submits the login form
HttpResponse formResponse = httpClient.execute(formPost);


Can someone please suggest where we are going wrong?
Thanks a lot for your time.

Regards,
Akshansh Khare

2 answers



permanent link
Boris Kuschel (331113) | answered Mar 15 '12, 8:38 a.m.
JAZZ DEVELOPER
On Thu, 15 Mar 2012 12:42:12 +0000, akshanshkhare wrote:

Hello Community,

We are building a dot net c# console application that reads data from a
REST Web Services.
The REST Web Service uses form authentication to authenticate the user.
But when we made the c# code this does not authenticate the server and
give error Time out Error in the Response.

C# Code (Time Out Error)

//Form authentication for REST Services HttpWebRequest RequestObject =
(HttpWebRequest)WebRequest.Create(new Uri(String.Format(serverURI
+"/j_security_check?username={0}&password={1}",
login, password)));
RequestObject.Method = WebRequestMethods.Http.Post;
RequestObject.ContentType =
"application/x-www-form-urlencoded";
RequestObject.Referer = clientUrl;
HttpWebResponse ResponseObject =
(HttpWebResponse)RequestObject.GetResponse();

We have also tried to authenticate the server using FORM Authentication
with CookieContainer.
But still it gives Time Out Error in the response.

C# Code with Cookie Container Used (Time Out
Error)



//Attempt to authenticate with the server with form and cookie
CookieContainer newCookieContainerObject = new CookieContainer();
HttpWebRequest Request =
(HttpWebRequest)WebRequest.Create(string.Format(serverURI +
"/j_security_check?j_username={0}&j_password={1}",
UserID, Password));
Request.CookieContainer = newCookieContainerObject;
Request.Method = WebRequestMethods.Http.Post;
Request.ContentType = "application/x-www-form-urlencoded";
HttpWebResponse ResponseObject =
(HttpWebResponse)Request.GetResponse();



We have the java code that authenticates the users and we are able to
extract the data from the REST Web Services.

Java Code (Works Fine)

// The server requires an authentication:
Create the login form HttpPost formPost = new
HttpPost(serverURI+"/j_security_check");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("username",
login));
nvps.add(new BasicNameValuePair("password",
password));
formPost.setEntity(new UrlEncodedFormEntity(nvps,
HTTP.UTF_8));

// Step (2): The client submits the login form HttpResponse formResponse
= httpClient.execute(formPost);


Can someone please suggest where we are going wrong?
Thanks a lot for your time.

Regards,
Akshansh Khare



I created a blog about this subjects and i have verified that the code
works. Take a look (Part 2): http://blog.boriskuschel.com/search/label/C%
23

This is how it is done in there:

HttpWebRequest formPost = (HttpWebRequest)
WebRequest.Create(serverURI + "/j_security_check");
formPost.Method = "POST";
formPost.Timeout = 30000;
formPost.CookieContainer = request.CookieContainer;
formPost.ContentType = "application/x-www-form-
urlencoded";
String output = "j_username=" + login +
"&j_password=" + password;
Byte[] outputBuffer = Encoding.UTF8.GetBytes(output);
formPost.ContentLength = outputBuffer.Length;
Stream stream = formPost.GetRequestStream();
stream.Write(outputBuffer, 0, outputBuffer.Length);
stream.Close();

--
Boris Kuschel
Jazz Jumpstart Team - IBM Software, Rational
My Blog: http://blog.boriskuschel.com/
--------------------------------------------------------------------------
This post represents my own opinion, thoughts, and conclusions based on my
knowledge of Jazz and the Jazz solutions.
For details on receiving official IBM Rational support please go to:
https://jazz.net/community/support/

permanent link
Akshansh Khare (56178) | answered May 24 '12, 4:20 a.m.
Hi kuschel,

Thanks for your reply.
Your Code was really a time and life saver.

Solution Link here : http://blog.boriskuschel.com/2012/02/c-with-visual-studio-and-rational-team.html

Cheers,
Akki

Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.