Michael Blake
Michael Blake

Reputation: 2158

How can you add AAD for Azure SQL Database in C#

How can we add AAD to Azure SQL Database using C#. Is there a NuGet package? https://azure.microsoft.com/en-gb/documentation/articles/sql-database-aad-authentication/

Upvotes: 0

Views: 1586

Answers (3)

Jake
Jake

Reputation: 31

Here is an example of the azure-sdk-for-net nuget being used by Azure PowerShell. This will show you how to get the tenant id and the Sid which is the Object Id of the Azure Active Directory user or group. Here is additional example of using the graph client in Azure PowerShell Active Directory Cmdlets.

Upvotes: 2

Michael Blake
Michael Blake

Reputation: 2158

Services.ServerAdministrators might work

https://github.com/Azure/azure-sdk-for-net/commit/7ca2d576d6e74ad1a18434f16146076ed1b384d4

https://github.com/Azure/azure-sdk-for-net/blob/21db6e5490e66af39a9c6dbf0ad10650d9ca037b/src/ResourceManagement/Sql/Sql.Tests/ScenarioTests/Sql2.ServerAdministratorScenarioTests.cs

You need to provide the Tenant ID and SID for the AD user. Not sure how to get this yet. I guess the Graph API.

Also, the admin parameter needs to be client.ServerAdministrators.CreateOrUpdateAsync(resourceGroupName, sqlServerName, "activeDirectory",

Upvotes: 0

Lybecker
Lybecker

Reputation: 628

Connecting to a Microsoft SQL Server – on-premise or SQL Azure does not require NuGet packages. Just use ADO.NET, which is part of the base class library. .NET framework version 4.6 is required.

To use Azure Active Directory integration (a preview feature) your first need to configure Azure AD and the SQL Azure instance

  1. Create and populate an Azure Active Directory
  2. Ensure your database is in Azure SQL Database V12
  3. Optional: Associate or change the active directory that is currently associated with your Azure Subscription
  4. Create an Azure Active Directory administrator for Azure SQL Server
  5. Configure your client computers
  6. Create contained database users in your database mapped to Azure AD identities
  7. Connect to your database by using Azure AD identities

Source

Then use ADO.NET as usual and use a trusted connection like so in the connection string

Data Source=youdatabase.database.windows.net; Authentication=Active Directory Integrated;

The web server must run in context of a user created in your Azure AD tenant.

Azure AD integration is not yet supported for Azure WebSites.

Upvotes: -1

Related Questions