How to access database by External Datasource using Service Principal Name(SPN)

Devendra Parmar 20 Reputation points
2024-07-21T14:42:53.0533333+00:00

I am seeking a solution for cross-database access via an external data source using Service Principal credentials. Specifically, I want to perform Transact-SQL queries from one database to another using Service Principal credentials. To achieve this, I am using Service Principal Name(SPN) for creating required Database Scoped Credentials (DSC) and External Datasource.

Here is how I am creating the DSC:

CREATE DATABASE SCOPED CREDENTIAL testManageAccessSPN2 WITH IDENTITY = 'CLIENT_ID' , SECRET = 'CLIENT_SECRET';

Where CLIENT_ID is a special id from the overview tab of App registration (Application (client) ID) And Secret = CLIENT_SECRET from the "Certificate & secrets"

Format:
CLIENT_ID = 00000000-0000-0000-0000-000000000000

CLIENT_SECRET = O7q8Q~otvRVdha.EmqEsyxxxxxxxxxxxxx

However, when I try to access the database using these credentials, I encounter the following error:

An error occurred while establishing connection to remote data source: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user '

I have tested these credentials by logging into SSMS, and they are working fine. Please assist me with a possible solution. Thanks.

Azure SQL Database
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,962 questions
Microsoft Entra External ID
Microsoft Entra External ID
A modern identity solution for securing access to customer, citizen and partner-facing apps and services. It is the converged platform of Azure AD External Identities B2B and B2C. Replaces Azure Active Directory External Identities.
2,908 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
22,060 questions
{count} votes

Accepted answer
  1. Nandan Hegde 32,416 Reputation points MVP
    2024-07-23T02:56:04.5966667+00:00

    Based on my understanding, only SQL auth is supported for cross database queries and AD auth is not supported.

    https://learn.microsoft.com/en-us/azure/azure-sql/database/elastic-query-getting-started-vertical?view=azuresql#database-scoped-master-key-and-credentials

    User's image

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Amira Bedhiafi 25,946 Reputation points
    2024-07-21T18:47:50.64+00:00

  2. LiHongMSFT-4306 27,961 Reputation points
    2024-07-22T02:09:35.6966667+00:00

    Hi @Devendra Parmar

    Here are some resolutions from this blog:

    Enable the option "Allow Azure services and resources to access this server" and to set the option "Public network access" to "Selected networks" in the remote Azure SQL Database server's firewall configuration.

    If "Allow Azure services" is not secure enough for your requirements, you could also try to create IP address firewall rules for each of the local databases. You need to catch the firewall errors and get the public IP address of the database that is attempting to log in. You could then add that IP in a firewall rule to the remote server and retry the query. This approach is relatively complex to implement, as it is a manual process and cannot be automated. It is also possible that the IP address may change in the future.

    If neither of these is a valid option, you may consider moving the Azure SQL Databases to an Azure SQL Managed Instance, which natively allows cross-database queries.  

    Best regards,

    Cosmog


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

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.