Mar 13 2023 06:34 AM - edited Mar 13 2023 07:09 AM
I have Windows Service hosted on-premises and Azure SQL server. How I can connect Azure SQL database in Windows Service using Passwordless ConnectionString (using Azure Active Directory - Integrated)?
Mar 13 2023 07:22 AM - edited Mar 13 2023 07:24 AM
# Register the Windows Service in Azure AD:
# Configure Azure SQL Server to allow Azure AD authentication:
# Grant the registered application access to the Azure SQL Server:
# Configure the Windows Service to use Azure AD authentication:
string tenantId = "<your-tenant-id>";
string clientId = "<your-client-id>";
string clientSecret = "<your-client-secret>";
string resource = "https://database.windows.net/";
var authenticationContext = new AuthenticationContext("https://login.microsoftonline.com/" + tenantId);
var credential = new ClientCredential(clientId, clientSecret);
var result = authenticationContext.AcquireTokenAsync(resource, credential).Result;
SqlConnection connection = new SqlConnection("<your-connection-string>");
connection.AccessToken = result.AccessToken;
Replace the placeholders with the actual values for your Azure AD tenant, registered application, and Azure SQL Server connection string.
By following these steps, you can enable a Windows Service running on-premises to use Azure AD authentication to connect to an Azure SQL Server instance.
Mar 14 2023 06:47 PM
You may referring this:
Azure SQL Database Passwordless Connections - Code Samples | Microsoft Learn
Mar 16 2023 02:57 AM