Skip to content

Commit 7eda529

Browse files
authored
Create azure-ad-odp-managed.cs
1 parent ee59d7f commit 7eda529

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// This is a simple ODP.NET, Managed Driver application that connects to an Oracle Autonomous Database
2+
// using a token obtained from Azure Active Directory (Azure AD).
3+
4+
// Azure.Identity can be obtained through NuGet Gallery.
5+
// It will include the Azure.Core and Azure.Identity namespaces.
6+
using System;
7+
using System.Threading;
8+
using Azure.Core;
9+
using Azure.Identity;
10+
using Oracle.ManagedDataAccess.Client;
11+
12+
namespace ConnectToOracleUsingAccessToken
13+
{
14+
class Program
15+
{
16+
static void Main()
17+
{
18+
try
19+
{
20+
// Retrieve an access token from Azure AD.
21+
string token = GetAccessToken();
22+
23+
// Create an instance of an OracleAccessToken. The access token needs to
24+
// be passed to the OracleAccessToken constructor as array of characters.
25+
var oracleAccessToken = new OracleAccessToken(token.ToCharArray());
26+
27+
// Create an instance of an OracleConnection object.
28+
// The developer must provide the appropriate data source setting.
29+
var connection = new OracleConnection("User Id=/;Data Source=<oracle>");
30+
31+
// tnsnames.ora, sqlnet.ora, and cwallet.sso must reside in the same
32+
// directory as the application executable. These files can be downloaded
33+
// from Oracle Cloud for the Oracle Autonomous DB instance.
34+
connection.TnsAdmin = @".\";
35+
36+
// Assign the OracleAccessToken to the AccessToken property on the
37+
// OracleConnection object.
38+
connection.AccessToken = oracleAccessToken;
39+
40+
// Open the connection.
41+
connection.Open();
42+
43+
// If Open() fails, it will throw an exception.
44+
Console.WriteLine("Open success.");
45+
46+
// Dispose the OracleConnection object.
47+
connection.Dispose();
48+
}
49+
catch (Exception ex)
50+
{
51+
Console.WriteLine(ex);
52+
}
53+
}
54+
55+
// Retrieves an Azure AD access token through the
56+
// Service Principal Auth flow using a client secret.
57+
static string GetAccessToken()
58+
{
59+
// The developer must configure the Azure AD parameters below.
60+
string clientId = "<client Id of app registration in Azure AD>";
61+
string tenantId = "<tenant Id of Azure AD>";
62+
string clientSecret = "<secret value of app registration in Azure AD>";
63+
string scope = "<scope of DB registration in Azure AD>";
64+
65+
// Create a TokenRequestContext object.
66+
var tokenRequestContext = new TokenRequestContext(new[] { scope });
67+
68+
// Create a ClientSecretCredential object.
69+
var credentials = new ClientSecretCredential(tenantId, clientId, clientSecret);
70+
71+
// Get the access token from Azure AD.
72+
AccessToken accessToken = credentials.GetToken(tokenRequestContext, default(CancellationToken));
73+
74+
// Return the access token.
75+
return accessToken.Token;
76+
}
77+
}
78+
}
79+
80+
/* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. */
81+
82+
/******************************************************************************
83+
*
84+
* You may not use the identified files except in compliance with The MIT
85+
* License (the "License.")
86+
*
87+
* You may obtain a copy of the License at
88+
* https://github.com/oracle/Oracle.NET/blob/master/LICENSE
89+
*
90+
* Unless required by applicable law or agreed to in writing, software
91+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
92+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
93+
*
94+
* See the License for the specific language governing permissions and
95+
* limitations under the License.
96+
*
97+
*****************************************************************************/

0 commit comments

Comments
 (0)