Skip to content

Managed Identity via connection string not working #815

@richardoliverpearce

Description

@richardoliverpearce

Describe the bug

Hi, I'm attempting use the new 2.1 release to provide Managed Identity connection string support to no success:

  • It works fine when using the method of creating an AccessToken using Microsoft.Identity.
  • The Managed Identity is System Assigned.
  • I've tried using
    • Authentication=Active Directory Managed Identity.
    • Authentication=Active Directory MSI.

Thanks

Exception message:
One or more errors occurred. (One or more errors occurred. (Received a non-retryable error. Identity Response Code: BadRequest, Response: {"StatusCode":400,"Message":"No Managed Identity found for specified ClientId/ResourceId/PrincipalId.","CorrelationId":"78dc831f-e5fd-4aa6-990d-7c798128be81"}))

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.WaitForPendingOpen()--- End of stack trace from previous location where exception was thrown

Trace Logging:
2020-11-23T12:09:07.069 [Information] <prov.DbConnectionHelper.ConnectionString_Set|API> 4, 'Server=myinstance.database.windows.net;Database=mydatabase;Authentication=Active Directory Managed Identity;'
2020-11-23T12:09:07.072 [Information] <sc.TdsParserStateObjectFactory.CreateTdsParserStateObject|INFO> AppContext switch 'Switch.Microsoft.Data.SqlClient.UseManagedNetworkingOnWindows' not enabled, native networking implementation will be used.
2020-11-23T12:09:07.073 [Information] <sc.TdsParser.Connect|SEC> Active Directory Managed Identity authentication
2020-11-23T12:09:07.077 [Information] <sc.TdsParser.Connect|SEC> Sending prelogin handshake
2020-11-23T12:09:07.077 [Information] <sc.TdsParser.SendPreLoginHandshake|INFO> ClientConnectionID 953610ca-f186-4b64-8bff-9f108703c5e1, ActivityID dcdd22a8-4c4b-42e9-bbf2-77c55bc5e643:1
2020-11-23T12:09:07.077 [Information] <sc.TdsParser.Connect|SEC> Consuming prelogin handshake
2020-11-23T12:09:07.088 [Information] <sc.TdsParser.TdsLogin|SEC> Sending federated authentication feature request
2020-11-23T12:09:07.088 [Information] <sc.TdsParser.TdsLogin|SEC> Sending federated authentication feature request
2020-11-23T12:09:07.090 [Information] <sc.TdsParser.TryRun|SEC> Received login acknowledgement token
2020-11-23T12:09:07.090 [Information] <sc.SqlInternalConnectionTds.LoginNoFailover> Routed to myinstance.database.windows.net
2020-11-23T12:09:07.091 [Information] <sc.TdsParserStateObjectFactory.CreateTdsParserStateObject|INFO> AppContext switch 'Switch.Microsoft.Data.SqlClient.UseManagedNetworkingOnWindows' not enabled, native networking implementation will be used.
2020-11-23T12:09:07.091 [Information] <sc.TdsParser.Connect|SEC> Active Directory Managed Identity authentication
2020-11-23T12:09:07.094 [Information] <sc.TdsParser.Connect|SEC> Sending prelogin handshake
2020-11-23T12:09:07.094 [Information] <sc.TdsParser.SendPreLoginHandshake|INFO> ClientConnectionID d504231e-8a7f-41bb-80be-5e4120b9dd01, ActivityID ff485a55-dafe-4901-b1fe-1e8879b18ff2:2
2020-11-23T12:09:07.095 [Information] <sc.TdsParser.Connect|SEC> Consuming prelogin handshake
2020-11-23T12:09:07.099 [Information] <sc.TdsParser.TdsLogin|SEC> Sending federated authentication feature request
2020-11-23T12:09:07.099 [Information] <sc.TdsParser.TdsLogin|SEC> Sending federated authentication feature request
2020-11-23T12:09:07.102 [Information] <sc.TdsParser.TryProcessFedAuthInfo> Processed FEDAUTHINFO token stream: STSURL: https://login.windows.net/C670B60D-C4C7-4DEC-8C1F-AC0DA3E72BEE, SPN: https://database.windows.net/
2020-11-23T12:09:07.103 [Information] <sc.SqlInternalConnectionTds.OnFedAuthInfo> 9, Generating federated authentication token
2020-11-23T12:09:07.103 [Information] AcquireTokenAsync | This environment is identified as an Azure App Service environment. Proceeding to acquire access token from Endpoint URL: http://127.0.0.1:41544/MSI/token/
2020-11-23T12:09:07.103 [Information] AcquireTokenAsync | Identity Object id received and will be used for acquiring access token
2020-11-23T12:09:07.107 [Information] AcquireTokenAsync | Request to acquire access token failed with status code BadRequest
2020-11-23T12:09:07.107 [Information] AcquireTokenAsync | Error occurred while acquiring access token: Received a non-retryable error. Identity Response Code: BadRequest, Response: {"StatusCode":400,"Message":"No Managed Identity found for specified ClientId/ResourceId/PrincipalId.","CorrelationId":"05e0bec7-08d4-499c-80b4-6a899cc38d26"}
2020-11-23T12:09:07.107 [Information] <sc.SqlError.SqlError|ERR> infoNumber=0, errorState=0, errorClass=20, errorMessage='Received a non-retryable error. Identity Response Code: BadRequest, Response: {"StatusCode":400,"Message":"No Managed Identity found for specified ClientId/ResourceId/PrincipalId.","CorrelationId":"05e0bec7-08d4-499c-80b4-6a899cc38d26"}', procedure='', lineNumber=0
2020-11-23T12:09:07.107 [Information] AcquireTokenAsync | Error occurred while acquiring access token: Received a non-retryable error. Identity Response Code: BadRequest, Response: {"StatusCode":400,"Message":"No Managed Identity found for specified ClientId/ResourceId/PrincipalId.","CorrelationId":"05e0bec7-08d4-499c-80b4-6a899cc38d26"}
2020-11-23T12:09:07.108 [Information] <sc.SqlInternalConnectionTds.LoginFailure|RES|CPOOL> 9
2020-11-23T12:09:07.109 [Information] <sc.SqlConnection.OpenAsyncRetry|Info> 4

To reproduce

Works as expected when attaching debugger to Azure Functions instance:

var credential = new DefaultAzureCredential();
var token = await credential.GetTokenAsync(new TokenRequestContext(new[] {"https://database.windows.net/.default"}), cancellationToken);

await using var connection = new SqlConnection("Server=myinstance.database.windows.net;Database=mydatabase;");
{
    connection.AccessToken = token.Token;
    await connection.OpenAsync(cancellationToken);
}

Fails when attaching to debugger Azure Functions instance:

await using var connection = new SqlConnection("Server=myinstance.database.windows.net;Database=mydatabase;Authentication=Active Directory Managed Identity;");
{
    await connection.OpenAsync(cancellationToken);
}

Expected behavior

Be able to call OpenAsync() without throwing an exception.

Further technical details

Microsoft.Data.SqlClient version: 2.1.0
.NET target: Core 3.1
SQL Server version: Azure SQL Database
Operating system: Azure Function App Runtime ~3 (tried both Windows and Linux plans)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions