I am running into intermittent login issue to an Azure SQL Database specifically from a Windows Server 2016 machine. I have an application that continuously, every 30 seconds, requests an Azure AD Token and uses that token in the sql connection object to connect to SQL Server and run a query. This loop intermittently fails with a login failed for user NT AUTHORITY\ANONYMOUS LOGON.
The failure seems to follow a pattern in which it works for 6 requests, then fails for 6 requests. I also changed the loop time to 15 seconds and the pattern doubled.
I ran the same console application on two different Windows 10 machines and a Windows Server 2012 R2 and it worked with no issues.
The code for the application is as follows (the variables to retrieve the token are not shown for security reasons):
while (true)
{
var token = Task.Run(() =>
SignInWithClientSecret("https://database.windows.net/", aadinstance,
applicationId, secret, true)).Result;
using (SqlConnection conn = new
SqlConnection(connectionstring))
{
conn.AccessToken = token.AccessToken;
//Console.WriteLine(token.AccessToken);
try
{
conn.Open();
using (var cmd = new SqlCommand(sqlcommand, conn))
{
var result = cmd.ExecuteNonQuery();
Console.WriteLine("Executed command");
}
}
catch (Exception ex)
{
Console.WriteLine("Error executing sql command: " +
ex.Message);
}
}
Thread.Sleep(30000);
}
The issue was due to the .NET Framework installed. I upgraded to 4.7.2 and it works.