我们有一个经典ASP加.Net应用程序 . 我们有一个.Net页面,将会话从Classic ASP转移到.Net . 此页面使用Socket类来调用生成会话数据XML的经典ASP页面 . 然后它通过Socket读取该数据并基于相同的方式创建.Net会话 .

我们在IIS 7.5中仅启用了Windows身份验证 .

上面的页面引发以下错误:

System.ArgumentOutOfRangeException:StartIndex不能小于零 . 参数名称:startIndex

在线:

xml = receive.Substring(receive.IndexOf("<?xml"), receive.Length - receive.IndexOf("<?xml"))

如果我们还启用匿名身份验证,它工作正常 . 但是,在执行此操作时,Windows身份验证将停止工作(按预期方式) . 所以我们的问题是,我们只需要启用Windows身份验证,但我们还需要此页面才能工作(目前在Windows Auth only模式下无效) .

我在下面给出了这个页面的代码 . 在这里,GetSession函数从另一个需要会话数据的.Net页面调用 . 然后,此页面通过调用下面的GetXML函数生成会话 .

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