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);
}
}