This documentation is archived and is not being maintained.

IUniversalAuthProvider Interface

 

Updated: July 10, 2017

Implement this interface to create a string access token. This access token will be used to set the System.Data.SqlClient.SqlConnection.AccessToken for any SqlConnection instances created when connecting to a database.

For more information about Active Directory Universal Authentication, see Configure and manage Azure Active Directory authentication with SQL Database or SQL Data Warehouse.

Namespace:   Microsoft.SqlServer.Dac
Assembly:  Microsoft.SqlServer.Dac (in Microsoft.SqlServer.Dac.dll)

public interface IUniversalAuthProvider

NameDescription
System_CAPS_pubmethodGetValidAccessToken()

Returns a valid access token that is used to set the AccessToken property of the SqlConnection.

The following example shows how to implement the IUniversalAuthProvider interface:

using System; 
using System.Collections.Generic; 
using Microsoft.SqlServer.Dac;
using Microsoft.IdentityModel.Clients.ActiveDirectory; 
using Microsoft.IdentityModel.Tokens.JWT; 
using System.Threading; 

namespace DacFx 
{ 
    class DacFxUniversalAuthProvider : IUniversalAuthProvider 
    { 
        private const string ClientID = "{your client Id}"; 
        private const string RedirectUri = "{your redirect URI}"; 

        private Object _accessTokenLock = new Object(); 
        private string _accessToken = string.Empty; 
        private DateTimeOffset _tokenExpirationTime; 

        public string ActiveDirectoryServerPrincipalName =  @"https://database.windows.net/"; 
        public string ActiveDirectoryAuthority = @"https://login.windows.net/common" 

        public DacFxAuthProvider() 
        {             
            _tokenExpirationTime = DateTime.Now; 
        } 

        private bool IsTokenExpired() 
        { 
            return _tokenExpirationTime.DateTime.Subtract(DateTime.UtcNow) < TimeSpan.FromMinutes(30); 
        } 

        public string GetValidAccessToken() 
        { 
            lock (_accessTokenLock) 
            { 
                if (string.IsNullOrEmpty(_accessToken) || IsTokenExpired()) 
                { 
                    var promptBehavior = string.IsNullOrEmpty(_accessToken) ? PromptBehavior.Always : PromptBehavior.Auto; 

                    try 
                    { 
                        AuthenticationContext authContext = new AuthenticationContext(ActiveDirectoryAuthority); 
                        Task<AuthenticationResult> authResult = null; 

                        Task.Run(() => 
                            authResult = authContext.AcquireTokenAsync(ActiveDirectoryServerPrincipalName, ClientID, new Uri(RedirectUri), 
                            new PlatformParameters(promptBehavior))).Wait(); 

                        _accessToken = authResult.Result.AccessToken; 
                        _tokenExpirationTime = authResult.Result.ExpiresOn;                         
                    } 
                    catch (Exception ex) 
                    { 
                        Console.WriteLine(ex.Message); 
                    } 
                } 

                return _accessToken; 
            } 
        } 
    } 

    public class DacpacService 
    { 
        public List<string> MessageList { get; set; } 

        public DacpacService() 
        { 
            MessageList = new List<string>(); 
        } 

        public bool ProcessDacPac(string connectionString, 
                                  string databaseName, 
                                  string dacpacName) 
        { 
            bool success = true; 

            MessageList.Add("*** Start of processing for " + 
                             databaseName); 
            DacFxAuthProvider token = new DacFxAuthProvider(); 

            var dacOptions = new DacDeployOptions(); 

            dacOptions.BlockOnPossibleDataLoss = false; 

            var dacServiceInstance = new DacServices(connectionString, token); 

            dacServiceInstance.ProgressChanged += 
                new EventHandler<DacProgressEventArgs>((s, e) => 
                    MessageList.Add(e.Message)); 
            dacServiceInstance.Message += 
                new EventHandler<DacMessageEventArgs>((s, e) => 
                    MessageList.Add(e.Message.Message)); 

            try 
            { 
                dacServiceInstance.Extract(dacpacName, databaseName, "test", new Version(1, 1)); 
            } 
            catch (Exception ex) 
            { 
                success = false; 
                MessageList.Add(ex.Message); 
            } 

            return success; 
        } 

        static void Main(string[] args) 
        { 
            DacpacService dacFx = new DacpacService(); 
            dacFx.ProcessDacPac("Data Source=tcp:testserver.database.windows.net;Initial Catalog=testdb;" 
                , "testdatabase", @"C:\tmp\test-api-token-app.dacpac"); 
        } 
    } 
} 
Return to top
Show: