Upgrading AppAuthentication to Azure.Identity in Console Application.

Pragya Sharma 45 Reputation points
2024-01-09T06:35:28.86+00:00

I have to upgrade the package AppAuthentication to Azure.Identity in my .Net core application. My project have different solutions in it. In web API I have successfully upgraded it by using Client Secret Credentials by using service principal and it is working fine there. It is able to access my azure sql database. But in my Console Application whose DLL I uses in various solutions I am having an issue. When I am using Default Azure Credentials, I am getting error in my web api in which section it access that DLL methods. When I am using Client Secret Credentials, I am getting sql connection error "login failed for user." Can you please tell me what approach I should follow and which credential I should use in my DLL code to connect with my azure sql database? I have service principal. (Earlier it was working fine when I was using azure service token provider for fetching credentials and token.)

Microsoft Security | Microsoft Entra | Microsoft Entra ID
0 comments No comments
{count} votes

Answer accepted by question author
  1. Anonymous
    2024-01-10T02:37:05.22+00:00

    Hi @Pragya Sharma , when upgrading from AppAuthentication to Azure.Identity, you can use the DefaultAzureCredential class to authenticate your application. The DefaultAzureCredential class tries multiple authentication methods in a specific order until it finds a successful authentication method. The order of authentication methods is:

    1. Environment variables
    2. Managed Identity
    3. Visual Studio
    4. Azure CLI
    5. Interactive

    To use the DefaultAzureCredential class in your Console Application, you can add the following code:

    using Azure.Identity;
    
    var credential = new DefaultAzureCredential();
    

    This will create a new instance of the DefaultAzureCredential class, which will try to authenticate using the methods listed above.

    Regarding the SQL connection error, it is possible that the service principal you are using does not have the necessary permissions to access the SQL database. You can check the permissions of the service principal by going to the SQL database in the Azure portal and checking the "Access control (IAM)" tab. Make sure that the service principal has the necessary permissions to access the database.

    If the service principal has the necessary permissions, you can try using the ClientSecretCredential class to authenticate your application. Here is an example of how to use the ClientSecretCredential class:

    using Azure.Identity;
    
    var credential = new ClientSecretCredential(
        "<tenant-id>",
        "<client-id>",
        "<client-secret>"
    );
    

    Replace <tenant-id>, <client-id>, and <client-secret> with the appropriate values for your service principal. Please let me know if you have any questions and I can help you further.

    If this answer helps you please mark "Accept Answer" so other users can reference it.

    Thank you,

    James

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

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