views:

226

answers:

1

I've got a Socket that is connected that I use to get HTTP header responses. If I skip authentication, everything works fine (unless the page requires authentication). But when I step into this code, it always throws an IOException on the AuthenticateAsClient line. The message is: of "Unable to read data from the transport connection: The connection was closed." I've tried both DefaultCredentials and DefaultNetworkCredentials.

Any ideas as to what I am missing? What is causing the connection to close?

thanks

if (Authenticate)
{

    NetworkStream clientStream = new NetworkStream(webSocket, false);
    NegotiateStream authStream = new NegotiateStream(clientStream);

    NetworkCredential netcred = CredentialCache.DefaultNetworkCredentials;


    try
    {
        authStream.AuthenticateAsClient(netcred,
            String.Empty,
            ProtectionLevel.None,
            TokenImpersonationLevel.Identification);

        if (!authStream.IsAuthenticated)
        {
            Console.WriteLine("Authentication failed");
            ErrorText = "Authentication using default credentials failed";
            return (HttpStatusCode)(-1);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
        ErrorText = ex.Message;
        return (HttpStatusCode)(-1);
    }
}
A: 

NegotiateStream doesn't perform HTTP authentication:

On Windows 95/98 systems, Windows NT LAN Manager (NTLM) is the protocol used for authentication. On other platforms the Kerberos protocol is used for authentication if both client and server support it; otherwise NTLM is used.

Have a look at the WebClient class which provides a Credentials and a ResponseHeaders property.

dtb
I'm connecting to a internal server that uses NTLM, so it should work. I don't want to use the WebClient class as it wraps WebResponse and WebRequest. I'm trying to stay low-level with Sockets calls.
HTTP and NTLM do not work together, HTTP uses HTTP (basic or digest) authentication. If you're doing HTTP, you should first try to get it working with WebClient. If I misread your original question and you need to use NTLM, please provide more information/code.
dtb
>HTTP and NTLM do not work togetherThat's untrue. Most corporate Intranets use NTLM/Negotiate running over HTTP. See http://www.rfc-editor.org/rfc/rfc4559.txt
EricLaw -MSFT-