Skip to content

SqlException: Login failed for user '<token-identified principal>'. #970

Description

@TresaL

Hi,
I'm using ef core 3.1.2 with .net core 3.1 which uses Microsoft.Data.SqlClient 2.1.1 to run scheduled tasks on azure function under consumption plan. I use azure managed user identity to connect to Azure SQL server. My connection string is like:
Server={serverModel.DatabaseServerName}; Authentication=Active Directory Managed Identity; User Id={userIdentityId}; Database={serverModel.DatabaseName};

I'm intermittently facing SQL Exception as below:
Microsoft.Data.SqlClient.SqlException (0x80131904): Login failed for user ''.
Inner exception Microsoft.Data.SqlClient.SqlException handled at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw:
at Microsoft.Data.ProviderBase.DbConnectionPool.CheckPoolBlockingPeriod (Microsoft.Data.SqlClient, Version=2.0.20168.4, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)
at Microsoft.Data.ProviderBase.DbConnectionPool.CreateObject (Microsoft.Data.SqlClient, Version=2.0.20168.4, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)
at Microsoft.Data.ProviderBase.DbConnectionPool.UserCreateRequest (Microsoft.Data.SqlClient, Version=2.0.20168.4, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)
at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection (Microsoft.Data.SqlClient, Version=2.0.20168.4, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)
at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection (Microsoft.Data.SqlClient, Version=2.0.20168.4, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)
at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection (Microsoft.Data.SqlClient, Version=2.0.20168.4, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)
at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal (Microsoft.Data.SqlClient, Version=2.0.20168.4, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)
at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection (Microsoft.Data.SqlClient, Version=2.0.20168.4, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)
at Microsoft.Data.SqlClient.SqlConnection.TryOpen (Microsoft.Data.SqlClient, Version=2.0.20168.4, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)
at Microsoft.Data.SqlClient.SqlConnection.Open (Microsoft.Data.SqlClient, Version=2.0.20168.4, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)
at Microsoft.Data.SqlClient.SqlConnection.Open (Microsoft.Data.SqlClient, Version=2.0.20168.4, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)
at Core.Repository.WritableDatabaseContext.ExecuteReader (Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=nullCore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: /home/vsts/work/1/s/Core/Repository/WritableDatabaseContext.csCore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: 83)

My ef db context is like:

public class MyDbContext : DbContext
{
    private string connectionString;

    public SnapCommsDbContext(string connectionString)
    {
        this.connectionString = connectionString;
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(this.connectionString,
            option => option.EnableRetryOnFailure(3, TimeSpan.FromSeconds(30), Enumerable.Empty<int>().ToList()));
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
}
}

The exception occurred at Microsoft.Data.ProviderBase.DbConnectionPool.CheckPoolBlockingPeriod. Why this method returns SQL exception with Login failed message? Probably, I might have reached any resource limitation on consumption plan?

Any inside on this is much appreciated.

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