Skip to content

Commit 77e16d7

Browse files
committed
Updated based on review comments
1 parent 3995f02 commit 77e16d7

1 file changed

Lines changed: 40 additions & 14 deletions

File tree

docs/connect/ado-net/sql/azure-active-directory-authentication.md

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ ms.author: v-jizho2
1313
ms.reviewer:
1414
---
1515

16-
1716
# Using Azure Active Directory Authentication with SqlClient
1817

1918
[!INCLUDE [appliesto-netfx-netcore-netst-md](../../../includes/appliesto-netfx-netcore-netst-md.md)]
@@ -24,7 +23,7 @@ This article describes how to connect to Azure SQL data sources using Azure Acti
2423

2524
Azure Active Directory (Azure AD) authentication uses identities in Azure Active Directory to access Azure SQL data sources such as Azure SQL Database, Azure SQL Managed Instance, and Azure Synapse Analytics. **Microsoft.Data.SqlClient** allows client applications to specify Azure AD credentials in different authentication modes when connecting to Azure SQL Database. By setting the `Authentication` connection property in the connection string, the client can choose a preferred Azure AD authentication mode according to the value provided. For more information about Azure AD authentication, see [Connecting to SQL Database By Using Azure Active Directory Authentication](/azure/azure-sql/database/authentication-aad-overview).
2625

27-
Starting with **Microsoft.Data.SqlClient** 2.0.0, support for `Active Directory Password` authentication, `Active Directory Integrated authentication`, and `Active Directory Interactive` authentication has been extended across .NET Framework, .NET Core, and .NET Standard. A new `Active Directory Service Principal` authentication mode is also added in SqlClient 2.0.0 that makes use of the client ID and secret of a service principal identity to accomplish authentication. More authentication modes are added in SqlClient 2.1.0 including `Active Directory Device Code Flow` and `Active Directory Managed Identity` (also known as `Active Directory MSI`). These new modes enable the application to acquire an access token to connect to the server. More information about all the Active Directory authentications are covered in the following sections.
26+
The early **Microsoft.Data.SqlClient** supports `Active Directory Password` for .NET Framework, .NET Core, and .NET Standard. It also supports `Active Directory Integrated` authentication and `Active Directory Interactive` authentication for .NET Framework. Starting with **Microsoft.Data.SqlClient** 2.0.0, support for `Active Directory Integrated authentication` and `Active Directory Interactive` authentication has been extended across .NET Framework, .NET Core, and .NET Standard. A new `Active Directory Service Principal` authentication mode is also added in SqlClient 2.0.0 that makes use of the client ID and secret of a service principal identity to accomplish authentication. More authentication modes are added in SqlClient 2.1.0 including `Active Directory Device Code Flow` and `Active Directory Managed Identity` (also known as `Active Directory MSI`). These new modes enable the application to acquire an access token to connect to the server. More information about all the Active Directory authentications are covered in the following sections.
2827

2928

3029
## Setting Azure Active Directory authentication in the connection string
@@ -41,12 +40,12 @@ When connecting to Azure SQL data sources with Azure AD authentication, the appl
4140
| Active Directory Managed Identity, <br>Active Directory MSI | Authenticate with an Azure AD identity using system-assigned or user-assigned managed identity | .NET Framework 4.6+, .NET Core 2.1+, .NET Standard 2.0+ | 2.1.0+ |
4241

4342
> [!NOTE]
44-
> <sup>1</sup> Before **Microsoft.Data.SqlClient** 2.0.0, the `Active Directory Integrated` and `Active Directory Interactive` authentications are only supported on .NET Framework 4.6+.
43+
> <sup>1</sup> Before **Microsoft.Data.SqlClient** 2.0.0, `Active Directory Integrated` and `Active Directory Interactive` authentications are only supported on .NET Framework 4.6+.
4544
4645

4746
## Connecting with Active Directory Password authentication
4847

49-
The `Active Directory Password` authentication mode supports authentication to Azure data sources with Azure AD for native or federated Azure AD users. When using this mode, user credentials must be provided in the connection string. The following example shows how to use `Active Directory Password` authentication.
48+
`Active Directory Password` authentication mode supports authentication to Azure data sources with Azure AD for native or federated Azure AD users. When using this mode, user credentials must be provided in the connection string. The following example shows how to use `Active Directory Password` authentication.
5049

5150
```c#
5251
// Use your own Server, Database, User Id, and Password.
@@ -60,13 +59,20 @@ using (SqlConnection conn = new SqlConnection(ConnectionString)) {
6059

6160
## Connecting with Active Directory Integrated authentication
6261

63-
To use the `Active Directory Integrated` authentication mode, you need to federate the on-premise Active Directory with Azure AD in the cloud. Federation can be done using Active Directory Federation Services (ADFS), for example. When logged in to a domain-joined machine, you can access Azure SQL data sources without being prompted for credentials with this mode. Username and password cannot be specified in the connection string. The Credential property of SqlConnection cannot be set in this mode. The following code snippet is an example of when `Active Directory Integrated` authentication is in use.
62+
To use `Active Directory Integrated` authentication mode, you need to federate the on-premise Active Directory with Azure AD in the cloud. Federation can be done using Active Directory Federation Services (ADFS), for example. When logged in to a domain-joined machine, you can access Azure SQL data sources without being prompted for credentials with this mode. Username and password cannot be specified in the connection string for .NET framework applications. Username is optional in the connection string for .NET Core and .NET Standard applications. The Credential property of SqlConnection cannot be set in this mode. The following code snippet is an example of when `Active Directory Integrated` authentication is in use.
6463

6564
```c#
6665
// Use your own Server and Database.
67-
string ConnectionString = @"Server=demo.database.windows.net; Authentication=Active Directory Integrated; Database=testdb";
66+
string ConnectionString1 = @"Server=demo.database.windows.net; Authentication=Active Directory Integrated; Database=testdb";
6867

69-
using (SqlConnection conn = new SqlConnection(ConnectionString)) {
68+
using (SqlConnection conn = new SqlConnection(ConnectionString1)) {
69+
conn.Open();
70+
}
71+
72+
// User Id is optional for .NET Core and .NET Standard
73+
string ConnectionString2 = @"Server=demo.database.windows.net; Authentication=Active Directory Integrated; Database=testdb; User Id=user@domain.com";
74+
75+
using (SqlConnection conn = new SqlConnection(ConnectionString2)) {
7076
conn.Open();
7177
}
7278
```
@@ -79,9 +85,16 @@ using (SqlConnection conn = new SqlConnection(ConnectionString)) {
7985
```c#
8086
// Use your own Server, Database, and User Id.
8187
// User Id is optional.
82-
string ConnectionString = @"Server=demo.database.windows.net; Authentication=Active Directory Interactive; Database=testdb; User Id=user@domain.com";
88+
string ConnectionString1 = @"Server=demo.database.windows.net; Authentication=Active Directory Interactive; Database=testdb; User Id=user@domain.com";
8389

84-
using (SqlConnection conn = new SqlConnection(ConnectionString)) {
90+
using (SqlConnection conn = new SqlConnection(ConnectionString1)) {
91+
conn.Open();
92+
}
93+
94+
// User Id is not provided.
95+
string ConnectionString2 = @"Server=demo.database.windows.net; Authentication=Active Directory Interactive; Database=testdb";
96+
97+
using (SqlConnection conn = new SqlConnection(ConnectionString2)) {
8598
conn.Open();
8699
}
87100
```
@@ -126,12 +139,12 @@ Since **Microsoft.Data.SqlClient** 2.1.0, the driver now supports authentication
126139
// Use your own Server and Database.
127140
string ConnectionString1 = @"Server=demo.database.windows.net; Authentication=Active Directory Managed Identity; Database=testdb";
128141

129-
string ConnectionString2 = @"Server=demo.database.windows.net; Authentication=Active Directory MSI; Database=testdb";
130-
131142
using (SqlConnection conn = new SqlConnection(ConnectionString1)) {
132143
conn.Open();
133144
}
134145

146+
string ConnectionString2 = @"Server=demo.database.windows.net; Authentication=Active Directory MSI; Database=testdb";
147+
135148
using (SqlConnection conn = new SqlConnection(ConnectionString2)) {
136149
conn.Open();
137150
}
@@ -144,12 +157,12 @@ The following example demonstrates `Active Directory Managed Identity` authentic
144157
// Use your own Server, Database, and User Id.
145158
string ConnectionString1 = @"Server=demo.database.windows.net; Authentication=Active Directory Managed Identity; User Id=ObjectIdOfManagedIdentity; Database=testdb";
146159

147-
string ConnectionString2 = @"Server=demo.database.windows.net; Authentication=Active Directory MSI; User Id=ObjectIdOfManagedIdentity; Database=testdb";
148-
149160
using (SqlConnection conn = new SqlConnection(ConnectionString1)) {
150161
conn.Open();
151162
}
152163

164+
string ConnectionString2 = @"Server=demo.database.windows.net; Authentication=Active Directory MSI; User Id=ObjectIdOfManagedIdentity; Database=testdb";
165+
153166
using (SqlConnection conn = new SqlConnection(ConnectionString2)) {
154167
conn.Open();
155168
}
@@ -198,6 +211,8 @@ The following example shows how to set an application client ID via a configurat
198211
</configuration>
199212
```
200213

214+
## Custom SQL Authentication Provider support
215+
201216
Given more flexibility, the client application can also use its own provider for AD authentication instead of using the _ActiveDirectoryAuthenticationProvider_ class. The custom authentication provider needs to be a subclass of _SqlAuthenticationProvider_ with overridden methods. The following example displays how to use a new authentication provider for `Active Directory Device Code Flow` authentication.
202217

203218
[!code-csharp [CustomDeviceCodeFlowAzureAuthenticationProvider#1](~/../sqlclient/doc/samples/CustomDeviceCodeFlowAzureAuthenticationProvider.cs#1)]
@@ -208,16 +223,27 @@ In addition, to improving the `Active Directory Interactive` authentication expe
208223
public class ActiveDirectoryAuthenticationProvider
209224
{
210225
// For .NET Framework targeted applications only
226+
// Sets a reference to the current System.Windows.Forms.IWin32Window that triggers the browser to be shown.
227+
// Used to center the browser pop-up onto this window.
211228
public void SetIWin32WindowFunc(Func<IWin32Window> iWin32WindowFunc);
212229

213230
// For .NET Standard targeted applications only
231+
// Sets a reference to the ViewController (if using Xamarin.iOS), Activity (if using Xamarin.Android) IWin32Window or IntPtr (if using .NET Framework).
232+
// Used for invoking the browser for Active Directory Interactive authentication.
214233
public void SetParentActivityOrWindowFunc(Func<object> parentActivityOrWindowFunc);
215234

216235
// For .NET Framework, .NET Core and .NET Standard targeted applications
236+
// Sets a callback method which is invoked with a custom Web UI instance that will let the user sign-in with Azure Active Directory, present consent if needed, and get back the authorization code.
237+
// Applicable when working with Active Directory Interactive authentication.
217238
public void SetAcquireAuthorizationCodeAsyncCallback(Func<Uri, Uri, CancellationToken, Task<Uri>> acquireAuthorizationCodeAsyncCallback);
239+
240+
// For .NET Framework, .NET Core and .NET Standard targeted applications
241+
// Clears cached user tokens from the token provider.
242+
public static void ClearUserTokenCache();
218243
}
219244
```
220245

221246

222247
## See Also
223-
[Application and service principal objects in Azure Active Directory](/azure/active-directory/develop/app-objects-and-service-principals)
248+
- [Application and service principal objects in Azure Active Directory](/azure/active-directory/develop/app-objects-and-service-principals)
249+
- [Authentication flows](/azure/active-directory/develop/msal-authentication-flows)

0 commit comments

Comments
 (0)