The situation I came across is when my web application receives a request and that request triggers a request/transaction using HttpWebRequest (System.Net) with NTLM authentication.
This transaction is logged as an error (401) which is the NTLM negotiation response despite the transaction actually giving 200.
Expected behavior
The transaction will be logged as success instead of the current error state
using System;
using System.Net;
using System.IO;
public class NtlmAuthExample
{
public static void Main()
{
string url = "https://your-api-endpoint.com/resource";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Credentials = CredentialCache.DefaultCredentials; // Use NTLM authentication
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode == HttpStatusCode.OK)
{
// Successfully authenticated and received a 200 response
using (Stream dataStream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(dataStream))
{
string responseFromServer = reader.ReadToEnd();
Console.WriteLine("Response received: " + responseFromServer);
// Log the successful transaction here
LogTransaction(true, response.StatusCode, responseFromServer);
}
}
else
{
// Handle other status codes if necessary
LogTransaction(false, response.StatusCode, "Unexpected status code");
}
}
}
catch (WebException ex)
{
if (ex.Response != null)
{
using (HttpWebResponse errorResponse = (HttpWebResponse)ex.Response)
{
if (errorResponse.StatusCode == HttpStatusCode.Unauthorized)
{
// This is part of the NTLM handshake, not an actual error
Console.WriteLine("NTLM negotiation in progress...");
// You can choose to log this as an informational message rather than an error
LogTransaction(false, errorResponse.StatusCode, "NTLM negotiation");
}
else
{
// Handle other web exceptions
LogTransaction(false, errorResponse.StatusCode, ex.Message);
}
}
}
else
{
// Handle other exceptions
LogTransaction(false, HttpStatusCode.InternalServerError, ex.Message);
}
}
}
private static void LogTransaction(bool isSuccess, HttpStatusCode statusCode, string message)
{
// Implement your logging logic here
// For example, you can use a logging framework like NLog, log4net, or simply write to a file
string logMessage = $"Transaction Success: {isSuccess}, Status Code: {statusCode}, Message: {message}";
Console.WriteLine(logMessage);
// Example: Write to a file
File.AppendAllText("transaction_log.txt", logMessage + Environment.NewLine);
}
}
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.