How to fix code throwing exception : Microsoft.Data.SqlClient.SqlException: 'Login failed for user '

Krishna Basavaraj K S 20 Reputation points
2025-01-03T00:29:43.2+00:00

Hi All,
I'm encountering an issue while trying to connect to an Azure SQL Database using Microsoft Entra Authentication (formerly Azure AD). I am using DefaultAzureCredential in my .NET application to authenticate and retrieve the access token, but while the token is successfully generated, I am getting the following error during authentication:

Error encountered:
*"Microsoft.Data.SqlClient.SqlException: 'Login failed for user '

Azure SQL Database
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,930 questions
SQL Server Other
Developer technologies C#
{count} votes

Accepted answer
  1. krishna basavaraj savasuddi 95 Reputation points
    2025-01-08T08:08:30.8866667+00:00

    @NIKHILA NETHIKUNTA , Since I am using dotnet framework 4.8, the solution was to specify the authentication as Active Directory Default within the connection string and also remove the unwanted below code as its taken care internally.

    var credential = new DefaultAzureCredential();

    var tokenRequestContext = new TokenRequestContext(new[] { "https://database.windows.net/.default" });

    var token = credential.GetToken(tokenRequestContext);

    connection.AccessToken = token.Token;


2 additional answers

Sort by: Most helpful
  1. krishna basavaraj savasuddi 95 Reputation points
    2025-01-08T08:12:36.13+00:00

    @NIKHILA NETHIKUNTA Since I am using dotnet framework 4.8, the solution was to specify the authentication as Active Directory Default within the connection string and also remove the unwanted below code as its taken care internally.

    var credential = new DefaultAzureCredential();

    var tokenRequestContext = new TokenRequestContext(new[] { "https://database.windows.net/.default" });

    var token = credential.GetToken(tokenRequestContext);

    connection.AccessToken = token.Token;

    0 comments No comments

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.