G
Guest
I have posted a similar message in 2 other forums but got no response. I
have spent more hours than I can count researching this. Can anyone provide
some insight...?
Our ASP.Net application needs to transparently log a user on to a separate
secure web site (PHP - not controlled by us). We want to save the user the
step of typing in his username and password and having to press submit.
I could accomplish this by using the <form action="myform.php"
method="post"> but I would have to store the password in a hidden field that
could be easily viewed.
I thought I was on the right track with the below code... but I cannot
figure out how to actually redirect the user to the page that appears after
login. (Response.redirect only shows the login page again, even though I
just posted the login values.)
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create("https://myServer/myPage.php");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
request.KeepAlive = true;
request.AllowAutoRedirect = true;
request.ContentType ="application/x-www-form-urlencoded";
string body = "logOnUserName=mek&userPwd=mypass&Submit=submit";
byte[] bytes = Encoding.ASCII.GetBytes(body);
request.ContentLength = bytes.Length;
StreamWriter stOut = new StreamWriter (request.GetRequestStream(),
System.Text.Encoding.ASCII);
stOut.Write(body);
stOut.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
I have also tried code presented on this forum (below), but this only brings
the information into my page, it does not post the username and password and
redirect:
private void DoPost()
{
String uriString ="https://myServer/myPage.php?";
System.Net.WebClient myWebClient = new System.Net.WebClient();
System.Collections.Specialized.NameValueCollection
System.Collections.Specialized.NameValueCollection myNameValueCollection =
new System.Collections.Specialized.NameValueCollection();
myNameValueCollection.Add("logOnUserName", "me");
myNameValueCollection.Add("userPwd", "mypass");
myNameValueCollection.Add("OrganizationId", "1234");
myNameValueCollection.Add("Submit", "");
myWebClient.UploadValues (uriString, "POST", myNameValueCollection)
Byte[] responseArray = myWebClient.UploadValues (uriString, "POST",
myNameValueCollection);
Label1.Text = "Response received was : " +
System.Text.Encoding.ASCII.GetString(responseArray);
}
Any suggestions would be greatly appreciated!
have spent more hours than I can count researching this. Can anyone provide
some insight...?
Our ASP.Net application needs to transparently log a user on to a separate
secure web site (PHP - not controlled by us). We want to save the user the
step of typing in his username and password and having to press submit.
I could accomplish this by using the <form action="myform.php"
method="post"> but I would have to store the password in a hidden field that
could be easily viewed.
I thought I was on the right track with the below code... but I cannot
figure out how to actually redirect the user to the page that appears after
login. (Response.redirect only shows the login page again, even though I
just posted the login values.)
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create("https://myServer/myPage.php");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
request.KeepAlive = true;
request.AllowAutoRedirect = true;
request.ContentType ="application/x-www-form-urlencoded";
string body = "logOnUserName=mek&userPwd=mypass&Submit=submit";
byte[] bytes = Encoding.ASCII.GetBytes(body);
request.ContentLength = bytes.Length;
StreamWriter stOut = new StreamWriter (request.GetRequestStream(),
System.Text.Encoding.ASCII);
stOut.Write(body);
stOut.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
I have also tried code presented on this forum (below), but this only brings
the information into my page, it does not post the username and password and
redirect:
private void DoPost()
{
String uriString ="https://myServer/myPage.php?";
System.Net.WebClient myWebClient = new System.Net.WebClient();
System.Collections.Specialized.NameValueCollection
System.Collections.Specialized.NameValueCollection myNameValueCollection =
new System.Collections.Specialized.NameValueCollection();
myNameValueCollection.Add("logOnUserName", "me");
myNameValueCollection.Add("userPwd", "mypass");
myNameValueCollection.Add("OrganizationId", "1234");
myNameValueCollection.Add("Submit", "");
myWebClient.UploadValues (uriString, "POST", myNameValueCollection)
Byte[] responseArray = myWebClient.UploadValues (uriString, "POST",
myNameValueCollection);
Label1.Text = "Response received was : " +
System.Text.Encoding.ASCII.GetString(responseArray);
}
Any suggestions would be greatly appreciated!