How to get JDBC driver Azure function to connect via Microsoft Entra?

Sagar Kapadia 0 Reputation points
2023-10-03T05:08:57.7833333+00:00

I am having issues posting code to my sample azure database. I am working on an azure function that inserts some data that starts from a POST http trigger. I am running into an issue where my connection string can be grabbed from local.settings.json BUT I keep getting errors in two ways. When my connection string uses JDBC (Microsoft Entra integrated authentication), I get this error in my console which doesnt make sense because I should not need to use this java.lang.IllegalArgumentException: KrbException: Cannot locate default realm.

If I use the JDBC (Microsoft Entra password authentication) with authentication=ActiveDirectoryPassword, the error is different and says "Failed to authenticate the user gmail.onmicrosoft.com in Active Directory (Authentication=ActiveDirectoryPassword)." I am not sure if this is caused because I am using a gmail domain or not. I can provide more code if its helpful

Azure SQL Database
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,911 questions
SQL Server Other
{count} votes

2 answers

Sort by: Most helpful
  1. MuthuKumaranMurugaachari-MSFT 22,441 Reputation points Moderator
    2023-10-03T14:45:32.7133333+00:00

    Sagar Kapadia Thanks for posting your question in Microsoft Q&A. Based on the description, you are looking to connect to Azure SQL database from Azure Functions using Microsoft Entra.

    If you have deployed your function code to Azure, then I suggest using Managed Identity and doc: https://learn.microsoft.com/en-us/azure/azure-functions/functions-identity-access-azure-sql-with-managed-identity provides detailed steps to enable system-assigned Managed Identity in Azure Function, grant access to the database and configure connection string with Active Directory Managed Identity authentication.

    However, if you are testing in your local environment, you might have to use ActiveDirectoryDefault authentication option and it follows the chain such as Environment, Managed Identity, IntelliJ/VS code/VS etc. for authentication. Check https://learn.microsoft.com/en-us/sql/connect/jdbc/connecting-using-azure-active-directory-authentication?view=sql-server-ver16#connect-using-activedirectorydefault-authentication-mode doc for different authentication options and info.

    From the exception above, it seems that you are trying to authenticate via Gmail, but only Azure AD (or Microsoft) accounts are supported with the authentication. So, I suggest you follow the above steps for using different authentication and let us know if any issues you face.

    I hope this helps with your questions.


  2. Sagar Kapadia 0 Reputation points
    2023-10-17T02:54:15.8666667+00:00

    Best thing to do for anyone who has this issue is just use sql authentication. Click on reset password in the azure portal for the database and use sql authentication if you dont know your password which is what I did. From there, it was straight forward especially from a quick connection on local.


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.