# NTLM negotiation logged as ERROR

**URL:** <https://discuss.elastic.co/t/ntlm-negotiation-logged-as-error/368877>\
**Category:** APM\
**Tags:** dotnet\
**Created:** [October 16, 2024, 2:04am UTC](https://discuss.elastic.co/t/ntlm-negotiation-logged-as-error/368877 "2024-10-16T02:04:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dsantos98](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dsantos98/32/137852_2.png) [@dsantos98](https://discuss.elastic.co/u/dsantos98)\
**Post date:** [October 16, 2024, 2:04am UTC](https://discuss.elastic.co/t/ntlm-negotiation-logged-as-error/368877/1 "2024-10-16T02:04:35Z")

</div>

### Environment

Windows  
.NET Framework 4.8 WebApp

### Describe the bug

The situation I came across is when my web application receives a request and that request triggers a request/transaction using HttpWebRequest ([System.Net](http://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

---

<div class="post-metadata">

**Author:** ![atul\_mohan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/atul_mohan/32/137776_2.png) [@atul\_mohan](https://discuss.elastic.co/u/atul_mohan)\
**Post date:** [January 30, 2025, 6:15am UTC](https://discuss.elastic.co/t/ntlm-negotiation-logged-as-error/368877/2 "2025-01-30T06:15:48Z")

</div>

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

```
