|
| 1 | +//This application demonstrates connecting .NET to Oracle database using Microsoft Entra ID/Azure Active |
| 2 | +// Directory single sign-on (SSO). It uses service principal authentication with either managed ODP.NET or |
| 3 | +// ODP.NET Core 23c or higher. |
| 4 | + |
| 5 | +// ODP.NET Azure AD SSO requires Oracle.ManagedDataAccess.Azure package from NuGet Gallery |
| 6 | +using Oracle.ManagedDataAccess.Azure; |
| 7 | +using Oracle.ManagedDataAccess.Client; |
| 8 | +using System.Security; |
| 9 | + |
| 10 | +//Set your Azure Active Directory parameters below and ODP.NET data source value |
| 11 | +string clientId = "<AZURE AD APP REGISTRATION CLIENT ID>"; |
| 12 | +string tenantId = "<AZURE AD TENANT ID>"; |
| 13 | +string clientSecret = "<AZURE AD APP REGISTRATION SECRET VALUE>"; |
| 14 | +string dbAppIdUri = "<AZURE AD PROTECTED RESOURCE ID>"; |
| 15 | +var conn = new OracleConnection("User Id=/;Data Source=<DATA SOURCE>;Connection Timeout=900;"); |
| 16 | + |
| 17 | +var secureSecret = new SecureString(); |
| 18 | +foreach (char c in clientSecret) |
| 19 | +{ |
| 20 | + secureSecret.AppendChar(c); |
| 21 | +} |
| 22 | +secureSecret.MakeReadOnly(); |
| 23 | + |
| 24 | +//Create Azure authentication token object and set its values. |
| 25 | +var tokenConfig = new AzureTokenAuthentication |
| 26 | +{ |
| 27 | + ClientId = clientId, |
| 28 | + TenantId = tenantId, |
| 29 | + ClientSecret = secureSecret, |
| 30 | + DatabaseApplicationIdUri = dbAppIdUri, |
| 31 | +}; |
| 32 | + |
| 33 | +//Set token authentication mode to Azure Service Principal and use Azure token authentication |
| 34 | +conn.TokenAuthentication = OracleTokenAuth.AzureServicePrincipal; |
| 35 | +conn.UseAzureTokenAuthentication(tokenConfig); |
| 36 | + |
| 37 | +try |
| 38 | +{ |
| 39 | + conn.Open(); |
| 40 | + Console.WriteLine("Connection opened successfully!"); |
| 41 | + using (OracleCommand cmd = conn.CreateCommand()) |
| 42 | + { |
| 43 | + //Retrieve authenticated identity value from database |
| 44 | + cmd.CommandText = "SELECT SYS_CONTEXT('USERENV', 'AUTHENTICATED_IDENTITY') FROM DUAL"; |
| 45 | + Console.WriteLine($"Authenticated identity: {cmd.ExecuteScalar().ToString()}"); |
| 46 | + } |
| 47 | +} |
| 48 | +catch (Exception ex) |
| 49 | +{ |
| 50 | + Console.WriteLine("Error: " + ex.Message); |
| 51 | +} |
| 52 | +conn.Dispose(); |
| 53 | + |
| 54 | +/* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. */ |
| 55 | + |
| 56 | +/****************************************************************************** |
| 57 | + * |
| 58 | + * You may not use the identified files except in compliance with The MIT |
| 59 | + * License (the "License.") |
| 60 | + * |
| 61 | + * You may obtain a copy of the License at |
| 62 | + * https://github.com/oracle/dotnet-db-samples/blob/master/LICENSE.txt |
| 63 | + * |
| 64 | + * Unless required by applicable law or agreed to in writing, software |
| 65 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 66 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 67 | + * |
| 68 | + * See the License for the specific language governing permissions and |
| 69 | + * limitations under the License. |
| 70 | + * |
| 71 | + *****************************************************************************/ |
0 commit comments