Forum Discussion
Connecting to Azure SQL Server from a Fabric Compute Notebook using Managed Identity
- Anonymous1 year ago
Hi hkaushik ,
I want to personally thank nilendraFabric for your active participation and the valuable solutions you have shared in the community forum. Your contributions truly make a significant impact.
Thank you for trying the provided approach. Since it did not resolve the issue, I kindly recommend raising a support ticket. This will enable a dedicated team to investigate and provide you with a resolution.To raise a support ticket for Fabric and Power BI, kindly follow the steps outlined in the following guide:
How to create a Fabric and Power BI Support ticket - Power BI | Microsoft Learn
If this post helps, please give us Kudos and consider marking it Accept as solution to assist other members in finding it more easily.
Thank you for being a part of Microsoft Fabric Community Forum!
Thanks,
Pallavi G.
Hello hkaushik
Workspace Identity is the most reliable option currently, as full support for Managed Identities in Fabric notebooks is limited.
When using Workspace Identity with Azure SQL Database, you don’t need to manually retrieve or pass additional tokens if the environment is properly configured. Instead, you need to grant the Workspace Identity access rights on the database itself.
connection_string = (
"Driver={ODBC Driver 18 for SQL Server};"
f"Server={server_name};"
f"Database={database_name};"
"Authentication=ActiveDirectoryMSI;"
"TrustServerCertificate=yes;"
)
Just fyi.
When using libraries like `mssparkutils` or `notebookutils` to retrieve tokens, these often return the user’s personal token instead of the Workspace Identity token.
is this is helpful please accept the solution
Hi nilendraFabric ,
I had tried that earlier too. It gives error:
Though, I am able to connect via pipeline which means there should be no access issue.
- nilendraFabric1 year agoSuper User
Did you tried this approach
Instead of pyodbc, use the Spark JDBC connector, which better integrates with Fabric environments:
jdbc_url = f"jdbc:sqlserver://{server_name}.database.windows.net:1433;database={database_name};encrypt=true;trustServerCertificate=false;Authentication=ActiveDirectoryMSI"
query = "(SELECT * FROM your_table) AS temp"df = spark.read \
.format("com.microsoft.sqlserver.jdbc.spark") \
.option("url", jdbc_url) \
.option("dbtable", query) \
.option("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver") \
.load()df.show()
====================
If you must use pyodbc, manually retrieve an access token for Workspace Identity:
from notebookutils import mssparkutils
# Get token for Azure SQL
token = mssparkutils.credentials.getToken(audience="https://database.windows.net/")# Build connection string
connection_string = (
"Driver={ODBC Driver 18 for SQL Server};"
f"Server={server_name};"
f"Database={database_name};"
"Encrypt=yes;TrustServerCertificate=yes;"
)# Connect using pyodbc
import pyodbc
conn = pyodbc.connect(connection_string, attrs_before={1256: token})- hkaushik1 year agoMicrosoft Employee
Had tried both the approaches already.
JDBC gives ManagedId related error:
PYODBC approach, the token retrieved there is my personal token and not the workspace's.