1
0
mirror of https://github.com/Microsoft/sql-server-samples.git synced 2025-12-08 14:58:54 +00:00
Files
sql-server-samples/samples/features/security/azure-active-directory-auth/token/Program.cs
2016-07-28 15:06:14 -07:00

38 lines
1.2 KiB
C#

using System;
using System.Data;
using System.Data.SqlClient;
namespace ClinicService
{
class Program
{
static void Main()
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder["Data Source"] = "aad-managed-demo.database.windows.net"; // replace with your server name
builder["Initial Catalog"] = "demo"; // replace with your database name
builder["Connect Timeout"] = 30;
string accessToken = TokenFactory.GetAccessToken();
if (accessToken == null)
{
Console.WriteLine("Fail to acuire the token to the database.");
}
using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
{
try
{
connection.AccessToken = accessToken;
connection.Open();
Console.WriteLine("Connected to the database");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Console.WriteLine("Please press any key to stop");
Console.ReadKey();
}
}
}