|
| 1 | +--- |
| 2 | +title: "Using Azure Active Directory| Microsoft Docs for SQL Server" |
| 3 | +ms.custom: "" |
| 4 | +ms.date: "01/28/2019" |
| 5 | +ms.prod: sql |
| 6 | +ms.prod_service: connectivity |
| 7 | +ms.reviewer: "" |
| 8 | +ms.technology: connectivity |
| 9 | +ms.topic: reference |
| 10 | +author: bazizi |
| 11 | +ms.author: v-beaziz |
| 12 | +--- |
| 13 | +# Using Azure Active Directory |
| 14 | +[!INCLUDE[appliesto-ss-asdb-asdw-pdw-md](../../../includes/appliesto-ss-asdb-asdw-pdw-md.md)] |
| 15 | + |
| 16 | +[!INCLUDE[Driver_OLEDB_Download](../../../includes/driver_oledb_download.md)] |
| 17 | + |
| 18 | +## Purpose |
| 19 | + |
| 20 | +Starting with version 18.2.1, Microsoft OLE DB Driver for SQL Server allows OLE DB applications to connect to an instance of Azure SQL Database using a federated identity. The new authentication methods include: |
| 21 | +- Azure Active Directory login ID and password |
| 22 | +- Azure Active Directory access token |
| 23 | +- Azure Active Directory integrated authentication |
| 24 | +- SQL login ID and password |
| 25 | + |
| 26 | +> [!NOTE] |
| 27 | +> When using the following Azure Active Directory options with the OLE DB driver, ensure that the [Active Directory Authentication Library for SQL Server](https://go.microsoft.com/fwlink/?LinkID=513072) has been installed: |
| 28 | +> - Azure Active Directory login ID and password |
| 29 | +> - Azure Active Directory integrated authentication |
| 30 | +> |
| 31 | +> ADAL isn't required for the other authentication methods or OLE DB operations. |
| 32 | +
|
| 33 | +> [!NOTE] |
| 34 | +> Using the following authentication modes with `DataTypeCompatibility` (or its corresponding property) set to `80` is **not** supported: |
| 35 | +> - Azure Active Directory authentication using login ID and password |
| 36 | +> - Azure Active Directory authentication using access token |
| 37 | +> - Azure Active Directory integrated authentication |
| 38 | +
|
| 39 | +## New connection string keywords and properties |
| 40 | +The following connection string keywords have been introduced to support Azure Active Directory authentication: |
| 41 | + |
| 42 | +|Connection keyword|Connection property|Description| |
| 43 | +|--- |--- |--- | |
| 44 | +|Access Token|SSPROP_AUTH_ACCESS_TOKEN|Specifies an access token to authenticate to Azure Active Directory. | |
| 45 | +|Authentication|SSPROP_AUTH_MODE|Specifies authentication method to use.| |
| 46 | + |
| 47 | +For more information about the new keywords/properties, see the following pages: |
| 48 | +- [Using Connection String Keywords with OLE DB Driver for SQL Server](../applications/using-connection-string-keywords-with-oledb-driver-for-sql-server.md) |
| 49 | +- [Initialization and Authorization Properties](../ole-db-data-source-objects/initialization-and-authorization-properties.md) |
| 50 | + |
| 51 | +## New encryption and certificate validation behavior |
| 52 | +This section discusses the changes in encryption and certificate validation behavior. These changes are **only** effective when using the new Authentication or Access Token connection string keywords (or their corresponding properties). |
| 53 | + |
| 54 | +### Encryption |
| 55 | +To improve security, when the new connection properties/keywords are used, the driver overrides the default encryption value by setting it to `yes`. Overriding happens at data source object initialization time. If encryption is set before initialization by any means, the value is respected and not overridden. |
| 56 | + |
| 57 | +> [!NOTE] |
| 58 | +> In ADO applications and in applications that obtain the `IDBInitialize` interface through `IDataInitialize::GetDataSource`, the Core Component implementing the interface explicitly sets encryption to its default value of `no`. As a result, the new authentication properties/keywords respect this setting and the encryption value **isn't** overridden. Therefore, it is **recommended** that these applications explicitly set `Use Encryption for Data=true` to override the default value. |
| 59 | +
|
| 60 | +### Certificate validation |
| 61 | +To improve security, the new connection properties/keywords respect the `TrustServerCertificate` setting (and its corresponding connection string keywords/properties) **independently of the client encryption setting**. As a result, server certificate is validated by default. |
| 62 | + |
| 63 | +> [!NOTE] |
| 64 | +> Certificate validation can also be controlled through the `Value` field of the `HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Client\SNI18.0\GeneralFlags\Flag2` registry entry. Valid values are `0` or `1`. The OLE DB driver chooses the most secure option between the registry and the connection property/keyword settings. That is, the driver will validate the server certificate as long as at least one of the registry/connection settings enables server certificate validation. |
| 65 | +
|
| 66 | +## GUI additions for Azure Active Directory |
| 67 | +The driver graphical user interface has been enhanced to allow Azure Active Directory authentication. For more information, see: |
| 68 | +- [SQL Server Login Dialog](../help-topics/sql-server-login-dialog.md) |
| 69 | +- [Universal Data Link (UDL) Configuration](../help-topics/data-link-pages.md) |
| 70 | + |
| 71 | +## Example connection strings |
| 72 | +This section shows examples of new and existing connection string keywords to be used with `IDataInitialize::GetDataSource` and `DBPROP_INIT_PROVIDERSTRING` property. |
| 73 | + |
| 74 | +### SQL authentication |
| 75 | +- Using `IDataInitialize::GetDataSource`: |
| 76 | + - New: |
| 77 | + > Provider=MSOLEDBSQL;Data Source=[server];Initial Catalog=[database];**Authentication=SqlPassword**;User ID=[username];Password=[password];Use Encryption for Data=true |
| 78 | + - Deprecated: |
| 79 | + > Provider=MSOLEDBSQL;Data Source=[server];Initial Catalog=[database];User ID=[username];Password=[password];Use Encryption for Data=true |
| 80 | +- Using `DBPROP_INIT_PROVIDERSTRING`: |
| 81 | + - New: |
| 82 | + > Server=[server];Database=[database];**Authentication=SqlPassword**;UID=[username];PWD=[password];Encrypt=yes |
| 83 | + - Deprecated: |
| 84 | + > Server=[server];Database=[database];UID=[username];PWD=[password];Encrypt=yes |
| 85 | +
|
| 86 | +### Integrated Windows authentication using Security Support Provider Interface (SSPI) |
| 87 | + |
| 88 | +- Using `IDataInitialize::GetDataSource`: |
| 89 | + - New: |
| 90 | + > Provider=MSOLEDBSQL;Data Source=[server];Initial Catalog=[database];**Authentication=ActiveDirectoryIntegrated**;Use Encryption for Data=true |
| 91 | + - Deprecated: |
| 92 | + > Provider=MSOLEDBSQL;Data Source=[server];Initial Catalog=[database];**Integrated Security=SSPI**;Use Encryption for Data=true |
| 93 | +- Using `DBPROP_INIT_PROVIDERSTRING`: |
| 94 | + - New: |
| 95 | + > Server=[server];Database=[database];**Authentication=ActiveDirectoryIntegrated**;Encrypt=yes |
| 96 | + - Deprecated: |
| 97 | + > Server=[server];Database=[database];**Trusted_Connection=yes**;Encrypt=yes |
| 98 | +
|
| 99 | +### AAD username and password authentication using ADAL |
| 100 | + |
| 101 | +- Using `IDataInitialize::GetDataSource`: |
| 102 | + > Provider=MSOLEDBSQL;Data Source=[server];Initial Catalog=[database];**Authentication=ActiveDirectoryPassword**;User ID=[username];Password=[password];Use Encryption for Data=true |
| 103 | +- Using `DBPROP_INIT_PROVIDERSTRING`: |
| 104 | + > Server=[server];Database=[database];**Authentication=ActiveDirectoryPassword**;UID=[username];PWD=[password];Encrypt=yes |
| 105 | +
|
| 106 | +### Integrated Azure Active Directory authentication using ADAL |
| 107 | + |
| 108 | +- Using `IDataInitialize::GetDataSource`: |
| 109 | + > Provider=MSOLEDBSQL;Data Source=[server];Initial Catalog=[database];**Authentication=ActiveDirectoryIntegrated**;Use Encryption for Data=true |
| 110 | +- Using `DBPROP_INIT_PROVIDERSTRING`: |
| 111 | + > Server=[server];Database=[database];**Authentication=ActiveDirectoryIntegrated**;Encrypt=yes |
| 112 | +
|
| 113 | +### Azure Active Directory authentication using an access token |
| 114 | + |
| 115 | +- Using `IDataInitialize::GetDataSource`: |
| 116 | + > Provider=MSOLEDBSQL;Data Source=[server];Initial Catalog=[database];**Access Token=[access token]**;Use Encryption for Data=true |
| 117 | +- Using `DBPROP_INIT_PROVIDERSTRING`: |
| 118 | + > Providing access token through `DBPROP_INIT_PROVIDERSTRING` isn't supported |
| 119 | +
|
| 120 | +## Azure Active Directory authentication code samples |
| 121 | + |
| 122 | +The following samples show the code required to connect to Azure Active Directory with connection keywords. |
| 123 | + |
| 124 | +### Access Token |
| 125 | +```cpp |
| 126 | +#include <string> |
| 127 | +#include <iostream> |
| 128 | +#include <msdasc.h> |
| 129 | + |
| 130 | +int main() |
| 131 | +{ |
| 132 | + wchar_t azureServer[] = L"server"; |
| 133 | + wchar_t azureDatabase[] = L"mydatabase"; |
| 134 | + wchar_t accessToken[] = L"eyJ0eXAiOi..."; |
| 135 | + IDBInitialize *pIDBInitialize = nullptr; |
| 136 | + IDataInitialize* pIDataInitialize = nullptr; |
| 137 | + HRESULT hr = S_OK; |
| 138 | + |
| 139 | + CoInitialize(nullptr); |
| 140 | + |
| 141 | + // Construct the connection string. |
| 142 | + std::wstring connString = L"Provider=MSOLEDBSQL;Data Source=" + std::wstring(azureServer) + L";Initial Catalog=" + |
| 143 | + std::wstring(azureDatabase) + L";Access Token=" + accessToken + L";Use Encryption for Data=true;"; |
| 144 | + hr = CoCreateInstance(CLSID_MSDAINITIALIZE, nullptr, CLSCTX_INPROC_SERVER, |
| 145 | + IID_IDataInitialize, reinterpret_cast<LPVOID*>(&pIDataInitialize)); |
| 146 | + if (FAILED(hr)) |
| 147 | + { |
| 148 | + std::cout << "Failed to create an IDataInitialize instance." << std::endl; |
| 149 | + goto Cleanup; |
| 150 | + } |
| 151 | + hr = pIDataInitialize->GetDataSource(nullptr, CLSCTX_INPROC_SERVER, connString.c_str(), |
| 152 | + IID_IDBInitialize, reinterpret_cast<IUnknown**>(&pIDBInitialize)); |
| 153 | + if (FAILED(hr)) |
| 154 | + { |
| 155 | + std::cout << "Failed to get data source object." << std::endl; |
| 156 | + goto Cleanup; |
| 157 | + } |
| 158 | + hr = pIDBInitialize->Initialize(); |
| 159 | + if (FAILED(hr)) |
| 160 | + { |
| 161 | + std::cout << "Failed to establish connection." << std::endl; |
| 162 | + goto Cleanup; |
| 163 | + } |
| 164 | + |
| 165 | +Cleanup: |
| 166 | + if (pIDBInitialize) |
| 167 | + { |
| 168 | + pIDBInitialize->Uninitialize(); |
| 169 | + pIDBInitialize->Release(); |
| 170 | + } |
| 171 | + if (pIDataInitialize) |
| 172 | + { |
| 173 | + pIDataInitialize->Release(); |
| 174 | + } |
| 175 | + |
| 176 | + CoUninitialize(); |
| 177 | +} |
| 178 | +``` |
| 179 | +### Active Directory Integrated |
| 180 | +```cpp |
| 181 | +#include <string> |
| 182 | +#include <iostream> |
| 183 | +#include <msdasc.h> |
| 184 | + |
| 185 | +int main() |
| 186 | +{ |
| 187 | + wchar_t azureServer[] = L"server"; |
| 188 | + wchar_t azureDatabase[] = L"mydatabase"; |
| 189 | + IDBInitialize *pIDBInitialize = nullptr; |
| 190 | + IDataInitialize* pIDataInitialize = nullptr; |
| 191 | + HRESULT hr = S_OK; |
| 192 | + |
| 193 | + CoInitialize(nullptr); |
| 194 | + |
| 195 | + // Construct the connection string. |
| 196 | + std::wstring connString = L"Provider=MSOLEDBSQL;Data Source=" + std::wstring(azureServer) + L";Initial Catalog=" + |
| 197 | + std::wstring(azureDatabase) + L";Authentication=ActiveDirectoryIntegrated;Use Encryption for Data=true;"; |
| 198 | + |
| 199 | + hr = CoCreateInstance(CLSID_MSDAINITIALIZE, nullptr, CLSCTX_INPROC_SERVER, |
| 200 | + IID_IDataInitialize, reinterpret_cast<LPVOID*>(&pIDataInitialize)); |
| 201 | + if (FAILED(hr)) |
| 202 | + { |
| 203 | + std::cout << "Failed to create an IDataInitialize instance." << std::endl; |
| 204 | + goto Cleanup; |
| 205 | + } |
| 206 | + hr = pIDataInitialize->GetDataSource(nullptr, CLSCTX_INPROC_SERVER, connString.c_str(), |
| 207 | + IID_IDBInitialize, reinterpret_cast<IUnknown**>(&pIDBInitialize)); |
| 208 | + if (FAILED(hr)) |
| 209 | + { |
| 210 | + std::cout << "Failed to get data source object." << std::endl; |
| 211 | + goto Cleanup; |
| 212 | + } |
| 213 | + hr = pIDBInitialize->Initialize(); |
| 214 | + if (FAILED(hr)) |
| 215 | + { |
| 216 | + std::cout << "Failed to establish connection." << std::endl; |
| 217 | + goto Cleanup; |
| 218 | + } |
| 219 | + |
| 220 | +Cleanup: |
| 221 | + if (pIDBInitialize) |
| 222 | + { |
| 223 | + pIDBInitialize->Uninitialize(); |
| 224 | + pIDBInitialize->Release(); |
| 225 | + } |
| 226 | + if (pIDataInitialize) |
| 227 | + { |
| 228 | + pIDataInitialize->Release(); |
| 229 | + } |
| 230 | + |
| 231 | + CoUninitialize(); |
| 232 | +} |
| 233 | +``` |
| 234 | + |
| 235 | +## See Also |
| 236 | +[Authorize access to Azure Active Directory web applications using the OAuth 2.0 code grant flow](https://go.microsoft.com/fwlink/?linkid=2072672) |
| 237 | + |
| 238 | +[Token-based authentication support for Azure SQL DB using Azure AD auth](https://go.microsoft.com/fwlink/?linkid=2068937) |
| 239 | + |
| 240 | +[Using Connection String Keywords with OLE DB Driver for SQL Server](../applications/using-connection-string-keywords-with-oledb-driver-for-sql-server.md) |
0 commit comments