Logging in to one site from another...

S

Stu

Hi,

I need to post log in details from one web site to another to log the user
in to one of two sites. The user will enter a username and password in to
textboxes on site 'A', and depending on the format of the username they will
be logged in to either site B or site C.

I am generating a form post from the code behind, but the login page I am
posting to returns a code 200 (OK), but no redirect URI - so the original
page just reloads. The form post scripts are working with a third party
site, but I can't see the code that handles the response at their end.

Can anyone point me in the right direction as to why this is not working -
or if this is the right approach!

Thanks in advance,

Stu


---/ snip /-----
'##### The call

Dim d As New StringDictionary
d.Add("Username", txtUserName.Text.Trim)
d.Add("Password", txtPassword.Text.Trim)
Dim MyCookie As String = ""
Response.Redirect(Utils.FormHelper.GetRedirectURL(Utils.FormHelper.PostData("http://MyDomain.com/login.aspx",
d), MyCookie))

'###### The routines

''' <summary>
'''
''' </summary>
''' <param name="SubmissionUrl"></param>
''' <param name="FormData"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function PostData(ByVal SubmissionUrl As String, ByVal
FormData As StringDictionary) As HttpWebResponse

' Build the authentication URI
Dim serverUri As Uri = New Uri(SubmissionUrl)

' Create the request
Dim request As HttpWebRequest = WebRequest.Create(serverUri)

' Set the headers
request.CookieContainer = New CookieContainer()
request.ContentType = "application/x-www-form-urlencoded"
request.Method = "POST"
request.KeepAlive = True
request.AllowAutoRedirect = False

Dim sb As New StringBuilder
Dim iCurrent As Integer = 1
For Each keypair As DictionaryEntry In FormData
sb.Append(keypair.Key & "=" & keypair.Value)
If iCurrent < FormData.Count Then
sb.Append("&")
End If
iCurrent += 1
Next

' Prepare the body of the request with the POST data
Dim body() As Byte = Encoding.UTF8.GetBytes(sb.ToString)

' Set the content length of the request
request.ContentLength = body.Length

' Send the request to the server
Using stream As Stream = request.GetRequestStream()
stream.Write(body, 0, body.Length)
End Using

Return request.GetResponse()
End Function

''' <summary>
''' This method retreives redirected URL from response header and
also passes back
''' any cookie (if there is any)
''' </summary>
''' <param name="webresponse"></param>
''' <param name="Cookie"></param>
''' <returns></returns>
Public Shared Function GetRedirectURL(ByVal webresponse As
HttpWebResponse, ByRef Cookie As String) As String
Dim uri As String = ""

Dim headers As WebHeaderCollection = webresponse.Headers
Dim code As HttpStatusCode
code = webresponse.StatusCode

If ((webresponse.StatusCode = HttpStatusCode.Found) Or
(webresponse.StatusCode = HttpStatusCode.Redirect) Or
(webresponse.StatusCode = HttpStatusCode.Moved) Or (webresponse.StatusCode =
HttpStatusCode.MovedPermanently)) Then
uri = headers("Location")
uri = uri.Trim()
End If

If (headers("Set-Cookie") IsNot Nothing) Then
Cookie = headers("Set-Cookie")
End If

Dim StartURI As String = "http:/"
If (uri.Length > 0 And uri.StartsWith(StartURI) = False) Then
uri = StartURI + uri
End If

Return uri
End Function
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top