[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 703
  • Last Modified:

login using WebRequest

See below code.  I copied this snippet of code from something I googled.  I want to login to a WEB page that uses a separate logon page.  When this executes, I get a status code of 200 and a status description of "OK", so all is well.  However, it does not login.   The response data I get back is just the login page.  It does not take me to the page it should after the login.  

Can anyone help me?  Why does it not login in?   Login1$UserName and Login1$Password are the ID's of the HTML elements of the login page.



Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim postDataString As String = ""
        postDataString += "Login1$UserName" & "=" & "xxxxx"
        postDataString += "&Login1$Password" & "=" & "yyyyyy"
       
        ' here the &username and &password
        ' tags should be the same as used in web page form which can be seen using View Source for the
        'html page

        Dim asciiEncoding As New System.Text.ASCIIEncoding()
        Dim postDataBytes As Byte() = asciiEncoding.GetBytes(postDataString)

        Dim uriLoginForm As New Uri("https://dhsweb.phila.gov/philadhsextranet/sitefinity/login.aspx?ReturnUrl=%2fPhilaDhsExtranet%2fSitefinity%2fDefault.aspx")

        Dim webRequest__1 As HttpWebRequest = DirectCast(WebRequest.Create(uriLoginForm), HttpWebRequest)
        webRequest__1.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
        ' webRequest__1.Referer = "https://dhsweb.phila.gov/PhilaDhsExtranet/OnlineApplications.aspx"

        ' actual website address here
        webRequest__1.KeepAlive = True
        webRequest__1.Method = "POST"
        webRequest__1.ContentType = "application/x-www-form-urlencoded"
        webRequest__1.ContentLength = postDataBytes.Length
        webRequest__1.AllowAutoRedirect = True
        webRequest__1.UserAgent = "Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"

        Dim outStream As System.IO.Stream = webRequest__1.GetRequestStream()
        outStream.Write(postDataBytes, 0, postDataBytes.Length)
        outStream.Close()

        Dim webResponse As HttpWebResponse = DirectCast(webRequest__1.GetResponse(), HttpWebResponse)
        Dim streamReader As StreamReader = New System.IO.StreamReader(webResponse.GetResponseStream())
        Dim respString As String = streamReader.ReadToEnd()

        Dim s As String = webResponse.StatusCode
        s = webResponse.StatusDescription

        SaveResponse(respString)

        streamReader.Close()
        webResponse.Close()

        MsgBox("Done")

    End Sub
0
HLRosenberger
Asked:
HLRosenberger
  • 5
  • 3
1 Solution
 
Alan WarrenCommented:
Not really SQL syntax, but maybe this will help:
<%@ Page Language="VB" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Text" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  Protected Sub Button1_Click(sender As Object, e As System.EventArgs)
    ' http://www.experts-exchange.com/Programming/Languages/SQL_Syntax/Q_27854166.html?cid=1752
    
    Dim UserName As String = "Alan"
    Dim Password As String = "shhh_secret"
    Dim Domain As String = "http://localhost/the-website/"
    
    ' The following folder and file should exist in your web
    ' The folder is protected by web.config deny unauthenticated users (<deny users="?"/>)
    Dim RequestUri As String = "http://localhost/the-website/securefolder/top-secret.aspx"
    
    ' Call the constructor  to create an instance of NetworkCredential with the 
    ' specified user name and password. 
    Dim myCredentials As New NetworkCredential(UserName, Password)
    
    ' Create a WebRequest with the specified URL.  
    Dim myWebRequest As WebRequest = WebRequest.Create(RequestUri)
    
    myCredentials.Domain = Domain
    
    myWebRequest.Credentials = myCredentials
    
    ' Send the request and wait for a response. 
    Dim myWebResponse As WebResponse = myWebRequest.GetResponse()
    With myWebResponse
      
      ' the response uri differs from the original requested uri, we are being redirected to the login page!
      If .ResponseUri <> myWebRequest.RequestUri Then
        
        ' try to login using the credentials we have
        If Membership.ValidateUser(UserName, Password) Then
          FormsAuthentication.RedirectFromLoginPage(UserName, True)
          
          ' get the web root path
          Dim strRoot As String = ""
          If Request.IsSecureConnection Then
            strRoot = String.Format("https://{0}{1}", Request.Url.Host, Page.ResolveUrl("~/"))
          Else
            strRoot = String.Format("http://{0}{1}", Request.Url.Host, Page.ResolveUrl("~/"))
          End If
          
          ' convert the original reqested uri to a relative path and redirect.
          Response.Redirect(RequestUri.Replace(strRoot, "~/"), False)
          
        End If
      End If
    End With

    ' Release the resources of the response object.
    myWebResponse.Close()
  
  End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    
    </div>
    </form>
</body>
</html>

Open in new window

Alan ";0)
0
 
HLRosenbergerAuthor Commented:
alanwarren

Oh, sorry... Posted in the wrong area....
0
 
HLRosenbergerAuthor Commented:
I've requested that this question be deleted for the following reason:

Posted in wrong area.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Alan WarrenCommented:
Maybe just move the question, rather than delete it, perhaps asp .net programming may be a more appropriate TA; invested a bit of time in this already.

Respectfully yours,
Alan
0
 
HLRosenbergerAuthor Commented:
OK  Thanks. So now what do I do with this question?
0
 
HLRosenbergerAuthor Commented:
OK.  I choose the wrong area.  My mistake.  So, what now to do with this question?
0
 
Alan WarrenCommented:
The possible solution I posted most definitely logs in using a WebRequest.

Respectfully yours,
Alan
0
 
HLRosenbergerAuthor Commented:
thanks
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now