使用AAD令牌的间歇性SQL连接问题(MSI方法)

时间:2020-04-19 23:43:47

标签: azure azure-active-directory azure-sql-database azure-web-app-service azure-managed-identity

使用App Service的托管服务身份遇到Azure SQL身份验证问题。如本文所述-Azure-sql-auth-with-msi

过去几个月中一切正常,但最近我们在尝试打开SQL连接时开始出现一些间歇性错误:

Unhandled Exception: System.Data.SqlClient.SqlException: Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling, SqlAuthenticationProviderManager sqlAuthProviderManager)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()

我早些时候怀疑这可能与某些代码更改有关。但是,能够通过简单的Web作业重现它,其唯一职责是获取令牌并打开SQL连接。

Wierd部分是,它不是连续失败,而是在一定间隔后随机失败(十分之二)。我尝试比较令牌的成功尝试和失败尝试,但是看不到任何区别。

示例Web作业代码:

using (SqlConnection conn = new SqlConnection
{
    ConnectionString = "data source=<server>.database.secure.windows.net;
                           Initial Catalog=<db>;
                           Persist Security Info=True;MultipleActiveResultSets=True",
    AccessToken = new AzureServiceTokenProvider()
                  .GetAccessTokenAsync("https://database.windows.net/").Result
})
{
    conn.Open();
    string cmdText = "SELECT SUSER_SNAME()";
    using (SqlCommand sqlCommand = new SqlCommand(cmdText, conn))
    {
        object obj = sqlCommand.ExecuteScalar();
    }
}

谢谢您的帮助!

0 个答案:

没有答案