We have a Classic ASP plus .Net application. We have a .Net page that transfers the session from Classic ASP to .Net. This page uses the Socket class to give call to a classic ASP page that generates XML of the session data. Then it reads that data via Socket and creates the .Net session based on the same.
We have only Windows Authentication enabled in IIS 7.5.
The above page throws the following error:
System.ArgumentOutOfRangeException: StartIndex cannot be less than zero. Parameter name: startIndex
On line:
xml = receive.Substring(receive.IndexOf("<?xml"), receive.Length - receive.IndexOf("<?xml"))
It works fine if we also enable the Anonymous Authentication. However, on doing that the Windows Authentication stops working (as expected). So our problem is, we need only Windows Authentication enabled, but we also need this page to work (which is currently not working in Windows Auth only mode).
I have given below the code from this page. Here, GetSession function get called from another .Net page that needs to session data. This page then generates the session by giving call to GetXML function below.
Public Function getSession(ByVal request As System.Web.HttpRequest, ByVal session As System.Web.SessionState.HttpSessionState)
session.RemoveAll()
'Define the URL and page to load the Session XML from
Dim sName As String = request.ServerVariables("SCRIPT_NAME")
Dim XMLServer As String = request.ServerVariables("SERVER_NAME")
Dim XMLPage As String = Left(sName, InStr(2, sName, "/")) & "ASPSessionXML.asp"
XMLServer = oNet.ComputerName & ".xxxxxxxx.com"
'Define an XMLDocument to allow easy XML tree navigation
Dim doc As XmlDocument = New XmlDocument()
'Load the document from the reader
doc.LoadXml(GetXML(XMLServer, XMLPage, request))
'Loop through the Session element's child nodes and set
'each Session object
Dim node As XmlNode
For Each node In doc.FirstChild.NextSibling.ChildNodes
session(node.Name.ToString()) = System.Web.HttpUtility.UrlDecode(node.InnerText.ToString())
Next
End Function
Public Function GetXML(ByVal server As String, ByVal page As String, ByVal request As System.Web.HttpRequest)
'create socket
Dim ipHostInfo As IPHostEntry = Dns.GetHostEntry(server)
Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
Dim ipe As IPEndPoint = New IPEndPoint(ipAddress, 80)
Dim socket As Socket = New Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp)
'connect socket to server
socket.Connect(ipe)
'Create the request to send to the server
Dim strRequest As String
strRequest = "GET /" & page & " HTTP/1.1" & vbCrLf
strRequest = strRequest & "Host: " & server & vbCrLf
strRequest = strRequest & "Connection: Close" & vbCrLf
strRequest = strRequest & "Cookie: " & request.Headers("Cookie") & vbCrLf
strRequest = strRequest & "User-Agent: " & request.Headers("User-Agent") & vbCrLf & vbCrLf
'Convert send data to bytes
Dim bytesSend As Byte() = Encoding.ASCII.GetBytes(strRequest)
'Send the data to the server
socket.Send(bytesSend, bytesSend.Length, 0)
'/**********************************************
'* Receive the returned data
'**********************************************/
'Declare variables for data receipt
Dim bytes(255) As Byte
Dim nBytes As Integer = 0
Dim receive As String = ""
Dim xml As String = ""
Do
nBytes = socket.Receive(bytes, bytes.Length, 0)
receive = receive & Encoding.ASCII.GetString(bytes, 0, nBytes)
Loop While (nBytes > 0)
'We have the page data, but it includes the headers
' Retrieve XML data from page response
xml = receive.Substring(receive.IndexOf("<?xml"), receive.Length - receive.IndexOf("<?xml"))
'Cleanup the socket
socket.Shutdown(SocketShutdown.Both)
socket.Close()
'Return the data
GetXML = xml
End Function