title: Authenticating in Azure SQL Database using Azure Active Directory B2C
body: <p>I'm having problems with authenticating to Azure SQL Database through Azure Active Directory.</p>
  
  <p>I am pretty much following the instructions I found here:
  [ <a href="https://azure.microsoft.com/en-us/documentation/articles/sql-database-aad-authentication/" rel="nofollow">https://azure.microsoft.com/en-us/documentation/articles/sql-database-aad-authentication/</a> ][Connecting to SQL Database By Using Azure Active Directory Authentication]</p>
  
  <p>I am able to authenticate with Azure Active Directory using localhost and OpenID. I am able to sign up, sign in, and log out. I used "fake@genericcompany.com" (actual email changed) as the user, and I can get an authorization_code and id_token by signing in. I have also added "fake@genericcompany.com" as the Active Directory admin of my SQL Database, and added my computer's IP address to the firewall settings.</p>
  
  <p>I have tried to authenticate with "fake@genericcompany.com" using Microsoft SQL Server Management Studio, but I received this error message:</p>
  
  <pre><code>===================================
  
  Cannot connect to abc.database.windows.net.
  
  ===================================
  
  Failed to authenticate the user fake@genericcompany.com in Active Directory (Authentication=ActiveDirectoryPassword).
  Error code 0xCAA90018; state 10
  Could not discover a user realm. (.Net SqlClient Data Provider)
  
  ------------------------------
  For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft%20SQL%20Server&amp;EvtSrc=MSSQLServer&amp;EvtID=0&amp;LinkId=20476
  
  ------------------------------
  Server Name: abc.database.windows.net
  Error Number: 0
  Severity: 11
  State: 0
  Procedure: ADALGetAccessToken
  
  
  ------------------------------
  Program Location:
  
     at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling)
     at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
     at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions)
     at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal&amp; connection)
     at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
     at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
     at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
     at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
     at System.Data.SqlClient.SqlConnection.Open()
     at Microsoft.SqlServer.Management.SqlStudio.Explorer.ObjectExplorerService.ValidateConnection(UIConnectionInfo ci, IServerType server)
     at Microsoft.SqlServer.Management.UI.ConnectionDlg.Connector.ConnectionThreadUser()
  </code></pre>
  
  <p>Here is my fake Azure setup:</p>
  
  <pre><code>Azure Active Directory B2C
  Directory domain: xyz.onmicrosoft.com
  
  Azure SQL Server
  Name: abc.database.windows.net
  Server version: V12
  Number of databases: 1
  Database name: def
  Dababase pricing tier: S0 Standard
  </code></pre>
  
  <p>I have also set up the subscription that contains the SQL Database and server to be within the same Active Directory stated above.</p>
  
  <p>I have read some stuff about "contained databases" and "contained database users", and I might need 2 databases: a "master database" and a "user database", but I don't understand all this, especially in the context of Azure SQL Database. Here is one of the links that I read, but don't fully understand:</p>
  
  <p>[ <a href="https://msdn.microsoft.com/library/ff929188.aspx" rel="nofollow">https://msdn.microsoft.com/library/ff929188.aspx</a> ][Contained Database Users - Making Your Database Portable]</p>
  
  <p>I'll post the other links below, since SO won't let me post more than 2 links.</p>