Skip to content

Unreachable network (169.254.169.254:80) to request Access Token outside Azure #1005

@johan-v-r

Description

@johan-v-r

Describe the bug

Trying to Authenticate Using Active Directory Managed Identity authentication from local machine (or any other environment outside Azure) gives the below exception.

Initially I created this SO issue that lead me to this breaking change/bug.

Exception message: Tried to get token using Managed Identity. Access token could not be acquired. A socket operation was attempted to an unreachable network. (169.254.169.254:80)

Stack trace:
   at Microsoft.Data.ProviderBase.DbConnectionPool.CheckPoolBlockingPeriod(Exception e)
   at Microsoft.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at Microsoft.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry, SqlConnectionOverrides overrides)
   at Microsoft.Data.SqlClient.SqlConnection.Open(SqlConnectionOverrides overrides)
   at Microsoft.Data.SqlClient.SqlConnection.Open()
   at NetLatest.Program.<TestConnectionAsync>d__2.MoveNext() in C:\SourceCode\NetLatest\NetLatest\Program.cs:line 43
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()

To reproduce

Executing this whilst logged in with AAD

static async Task TestConnectionAsync(string server, string db)
{
	// Use your own server and database.
	string ConnectionString1 = @$"Server={server}; Database={db}; Authentication=Active Directory Managed Identity";

	using (SqlConnection conn = new SqlConnection(ConnectionString1))
	{
		conn.Open();
	}
}

Expected behavior

Get the token... This should be possible from outside Azure right?
The following works 100% like the original tutorial suggested.

static async Task TestConnectionAsync(string server, string db)
{
	// Use your own server and database.
	var connString = @$"Server={server}; Database={db}";
	var token = await new AzureServiceTokenProvider().GetAccessTokenAsync("https://database.windows.net");

	using (SqlConnection conn = new SqlConnection(connString))
	{
		conn.AccessToken = token;
		conn.Open();
	}
}

Further technical details

Microsoft.Data.SqlClient version: 2.1.0
.NET target: 5.0
SQL Server version: Azure SQL
Operating system: Windows 10

Additional context
The environment is fully connected to Azure SQL using AAD, but not running in Azure.

Metadata

Metadata

Assignees

No one assigned

    Labels

    By Design 🎨Issues due to driver feature design and will not be fixed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions