Please Help: Posting and redirecting to another form.

Discussion in 'ASP .Net' started by Guest, Mar 30, 2005.

  1. Guest

    Guest 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)
    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(),
    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

    private void DoPost()
    String uriString ="https://myServer/myPage.php?";
    System.Net.WebClient myWebClient = new System.Net.WebClient();
    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",

    Label1.Text = "Response received was : " +

    Any suggestions would be greatly appreciated!
    Guest, Mar 30, 2005
    1. Advertisements

  2. Guest

    Elton Wang Guest

    Hi Denise,

    I suppose the key point is to put Response content from
    php page (HttpWebResponse) into Response of your page

    You can try following code

    //>>>>... how to do I send the user to the page that

    // Get stream of php page Response
    System.IO.BinaryReader phpStream = new BinaryReader
    // prepare an byte array for php page content
    byte[] phpBytes = new bytes[phpStream.Length];
    // get the content
    phpStream.Read(phpBytes, 0, phpStream.Length);
    // clear current page
    // write php page content to current page
    // Sends all currently buffered output to the client,
    stops execution of the page


    Elton Wang

    = "logOnUserName=mek&userPwd=mypass&Submit=submit";
    Elton Wang, Mar 30, 2005
    1. Advertisements

  3. Guest

    Guest Guest


    Thanks for your reply. I am trying to implement your suggestions, but I get
    compile errors on "phpStream.Length", 'System.IO.BinaryReader' does not
    contain a definition for 'Length'. I don't know enough about binary readers
    to fix this. Do I need an interim step?


    Guest, Mar 30, 2005
  4. Guest

    Elton Wang Guest

    Hi Denise,

    You can change BinaryReader to BufferedStream. The
    BufferedStream class has Length property and can also
    perform Read operation.

    I am still thinking your goal. Actually my code only
    present content that HttpWebRequest object gets from its
    request page. Hence suppose your HttpWebRequest object
    successfully log in request page, it doesn't redirect to
    that page. Instead it shows contents of the page. It's not
    what you exactly want.

    Redirect direct from server (Response.Redirect(url))
    doesn't help at all. Because it's Request object doesn't
    provide actual content, such as userID, password,
    although they are in Request object.

    I was just wondering why don't you let user log in
    directly to that site?


    on "phpStream.Length", 'System.IO.BinaryReader' does not
    Elton Wang, Mar 31, 2005
  5. Guest

    Guest Guest

    It is a requirement of our application. When the user logs into the parent
    site, the client wants their users to be able to access the affiliated site
    without having to remember another login.. a convenience for their users. We
    have access to all the information about their login.

    On this project, I am the developer, not the designer. Someone sold this
    idea to the client - I have to make it happen. Is there ANYTHING else you
    can think of...???


    Guest, Mar 31, 2005
  6. Guest

    Elton Wang Guest

    So in your page, you only track user log info then
    redirect to target page that's it. Or you need also to
    track if log on successful or failure?


    Elton Wang, Mar 31, 2005
  7. Guest

    Guest Guest

    I would be happy just to redirect them. If I can determine if the logon was
    sucessful, that would be great, but not absolutely necesssary.


    Guest, Mar 31, 2005
  8. Guest

    Elton Wang Guest

    I have an idea. It lets page to rebound to client first.
    Then on client-side, change the form's request URL to
    target URL, and automatically submit. You can try this
    idea by following method:

    private void Rebound(string Url)
    string txtPw = this.txtPw.Text;
    string PWclientID = this.txtPw.ClientID;
    string txtUser = this.txtUser.Text;
    string UserclientID = this.txtUser.ClientID;
    string scriptString = "<script
    language=JavaScript> ";
    scriptString += "var theform;";
    scriptString += "if
    (\"microsoft\") > -1) {";
    scriptString += "theform = document.Form1;";
    scriptString += "}else {";
    scriptString += "theform = document.forms[\"Form1
    scriptString += "}theform.action =\"" + Url
    + "\";";
    scriptString += "theform." + PWclientID
    + ".value='" + txtPw + "';";
    scriptString += "theform." + UserclientID
    + ".value='" + txtUser + "';";
    scriptString += "theform.submit();";
    scriptString += "</script>";


    Elton Wang, Mar 31, 2005
  9. Guest

    Guest Guest


    I tried this solution and it does work. My only concern is that if a user
    navigates back to this window and does a viewsource, the password will
    be visible in the javascript code... but the page was dynamically created on
    his machine so chances of security problems would be minimized.

    I still wish I could get the redirect from the server side working after the
    post ... but this may have to do. Thank so much for your great ideas.


    Guest, Apr 4, 2005
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.