Skip to content

Commit 25301d0

Browse files
authored
Merge pull request #217481 from David-Engel/patch-1
Update to Microsoft.Data.SqlClient
2 parents 78c27db + 059a65b commit 25301d0

File tree

1 file changed

+9
-38
lines changed

1 file changed

+9
-38
lines changed

articles/active-directory/managed-identities-azure-resources/tutorial-windows-vm-access-sql.md

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ This section shows how to create a contained user in the database that represent
6363
- [Universal Authentication with SQL Database and Azure Synapse Analytics (SSMS support for MFA)](/azure/azure-sql/database/authentication-mfa-ssms-overview)
6464
- [Configure and manage Azure Active Directory authentication with SQL Database or Azure Synapse Analytics](/azure/azure-sql/database/authentication-aad-configure)
6565

66-
SQL DB requires unique Azure AD display names. With this, the Azure AD accounts such as users, groups and Service Principals (applications), and VM names enabled for managed identity must be uniquely defined in AAD regarding their display names. SQL DB checks the Azure AD display name during T-SQL creation of such users and if it is not unique, the command fails requesting to provide a unique Azure AD display name for a given account.
66+
SQL DB requires unique Azure AD display names. With this, the Azure AD accounts such as users, groups and Service Principals (applications), and VM names enabled for managed identity must be uniquely defined in Azure AD regarding their display names. SQL DB checks the Azure AD display name during T-SQL creation of such users and if it is not unique, the command fails requesting to provide a unique Azure AD display name for a given account.
6767

6868
**To create a contained user:**
6969

@@ -101,50 +101,21 @@ Code running in the VM can now get a token using its system-assigned managed ide
101101
102102
## Access data
103103
104-
This section shows how to get an access token using the VM's system-assigned managed identity and use it to call Azure SQL. Azure SQL natively supports Azure AD authentication, so it can directly accept access tokens obtained using managed identities for Azure resources. You use the **access token** method of creating a connection to SQL. This is part of Azure SQL's integration with Azure AD, and is different from supplying credentials on the connection string.
104+
This section shows how to get an access token using the VM's system-assigned managed identity and use it to call Azure SQL. Azure SQL natively supports Azure AD authentication, so it can directly accept access tokens obtained using managed identities for Azure resources. This method doesn't require supplying credentials on the connection string.
105105
106-
Here's a .NET code example of opening a connection to SQL using an access token. The code must run on the VM to be able to access the VM's system-assigned managed identity's endpoint. **.NET Framework 4.6** or higher or **.NET Core 2.2** or higher is required to use the access token method. Replace the values of AZURE-SQL-SERVERNAME and DATABASE accordingly. Note the resource ID for Azure SQL is `https://database.windows.net/`.
106+
Here's a .NET code example of opening a connection to SQL using Active Directory Managed Identity authentication. The code must run on the VM to be able to access the VM's system-assigned managed identity's endpoint. **.NET Framework 4.6.2** or higher or **.NET Core 3.1** or higher is required to use this method. Replace the values of AZURE-SQL-SERVERNAME and DATABASE accordingly and add a NuGet reference to the Microsoft.Data.SqlClient library.
107107

108108
```csharp
109-
using System.Net;
110-
using System.IO;
111-
using System.Data.SqlClient;
112-
using System.Web.Script.Serialization;
113-
114-
//
115-
// Get an access token for SQL.
116-
//
117-
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://database.windows.net/");
118-
request.Headers["Metadata"] = "true";
119-
request.Method = "GET";
120-
string accessToken = null;
109+
using Microsoft.Data.SqlClient;
121110
122111
try
123112
{
124-
// Call managed identities for Azure resources endpoint.
125-
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
126-
127-
// Pipe response Stream to a StreamReader and extract access token.
128-
StreamReader streamResponse = new StreamReader(response.GetResponseStream());
129-
string stringResponse = streamResponse.ReadToEnd();
130-
JavaScriptSerializer j = new JavaScriptSerializer();
131-
Dictionary<string, string> list = (Dictionary<string, string>) j.Deserialize(stringResponse, typeof(Dictionary<string, string>));
132-
accessToken = list["access_token"];
133-
}
134-
catch (Exception e)
135-
{
136-
string errorText = String.Format("{0} \n\n{1}", e.Message, e.InnerException != null ? e.InnerException.Message : "Acquire token failed");
137-
}
138-
139113
//
140-
// Open a connection to the server using the access token.
114+
// Open a connection to the server using Active Direcotry Managed Identity authentication.
141115
//
142-
if (accessToken != null) {
143-
string connectionString = "Data Source=<AZURE-SQL-SERVERNAME>; Initial Catalog=<DATABASE>;";
144-
SqlConnection conn = new SqlConnection(connectionString);
145-
conn.AccessToken = accessToken;
146-
conn.Open();
147-
}
116+
string connectionString = "Data Source=<AZURE-SQL-SERVERNAME>; Initial Catalog=<DATABASE>; Authentication=Active Directory Managed Identity; Encrypt=True";
117+
SqlConnection conn = new SqlConnection(connectionString);
118+
conn.Open();
148119
```
149120

150121
>[!NOTE]
@@ -177,7 +148,7 @@ Alternatively, a quick way to test the end-to-end setup without having to write
177148
178149
```powershell
179150
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
180-
$SqlConnection.ConnectionString = "Data Source = <AZURE-SQL-SERVERNAME>; Initial Catalog = <DATABASE>"
151+
$SqlConnection.ConnectionString = "Data Source = <AZURE-SQL-SERVERNAME>; Initial Catalog = <DATABASE>; Encrypt=True;"
181152
$SqlConnection.AccessToken = $AccessToken
182153
$SqlConnection.Open()
183154
```

0 commit comments

Comments
 (0)