Skip to content

Commit dcd3da7

Browse files
Feedback applied
1 parent 11ec2c2 commit dcd3da7

File tree

1 file changed

+26
-32
lines changed

1 file changed

+26
-32
lines changed

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ActiveDirectoryAuthenticationProvider.cs

+26-32
Original file line numberDiff line numberDiff line change
@@ -118,48 +118,51 @@ public override async Task<SqlAuthenticationToken> AcquireTokenAsync(SqlAuthenti
118118

119119
string scope = parameters.Resource.EndsWith(s_defaultScopeSuffix) ? parameters.Resource : parameters.Resource + s_defaultScopeSuffix;
120120
string[] scopes = new string[] { scope };
121+
TokenRequestContext tokenRequestContext = new(scopes);
121122

122-
string authority = parameters.Authority;
123-
int seperatorIndex = parameters.Authority.LastIndexOf('/');
124-
125-
// Authority Url does not always contain "<tenantId>", e.g. for Kusto cluster dbs
126-
// Optionally split Tenant Id in such cases.
127-
if (Guid.TryParse(parameters.Authority.Substring(seperatorIndex + 1), out Guid tenantId))
128-
{
129-
authority = parameters.Authority.Remove(seperatorIndex + 1);
130-
}
123+
/* We split audience from Authority URL here. Audience can be one of the following:
124+
* The Azure AD authority audience enumeration
125+
* The tenant ID, which can be:
126+
* - A GUID (the ID of your Azure AD instance), for single-tenant applications
127+
* - A domain name associated with your Azure AD instance (also for single-tenant applications)
128+
* One of these placeholders as a tenant ID in place of the Azure AD authority audience enumeration:
129+
* - `organizations` for a multitenant application
130+
* - `consumers` to sign in users only with their personal accounts
131+
* - `common` to sign in users with their work and school accounts or their personal Microsoft accounts
132+
*
133+
* MSAL will throw a meaningful exception if you specify both the Azure AD authority audience and the tenant ID.
134+
* If you don't specify an audience, your app will target Azure AD and personal Microsoft accounts as an audience. (That is, it will behave as though `common` were specified.)
135+
* More information: https://docs.microsoft.com/azure/active-directory/develop/msal-client-application-configuration
136+
**/
131137

132-
TokenRequestContext tokenRequestContext = new(scopes);
138+
int seperatorIndex = parameters.Authority.LastIndexOf('/');
139+
string authority = parameters.Authority.Remove(seperatorIndex + 1);
140+
string audience = parameters.Authority.Substring(seperatorIndex + 1);
133141
string clientId = string.IsNullOrWhiteSpace(parameters.UserId) ? null : parameters.UserId;
134-
string tenant = tenantId == Guid.Empty ? null : tenantId.ToString();
135142

136143
if (parameters.AuthenticationMethod == SqlAuthenticationMethod.ActiveDirectoryDefault)
137144
{
138145
DefaultAzureCredentialOptions defaultAzureCredentialOptions = new()
139146
{
140147
AuthorityHost = new Uri(authority),
148+
SharedTokenCacheTenantId = audience,
149+
VisualStudioCodeTenantId = audience,
150+
VisualStudioTenantId = audience,
141151
ExcludeInteractiveBrowserCredential = true // Force disabled, even though it's disabled by default to respect driver specifications.
142152
};
153+
143154
// Optionally set clientId when available
144155
if (clientId != null)
145156
{
146157
defaultAzureCredentialOptions.ManagedIdentityClientId = clientId;
147158
defaultAzureCredentialOptions.SharedTokenCacheUsername = clientId;
148159
}
149-
// Optionally set tenantId when available
150-
if (tenant != null)
151-
{
152-
defaultAzureCredentialOptions.InteractiveBrowserTenantId = tenant;
153-
defaultAzureCredentialOptions.SharedTokenCacheTenantId = tenant;
154-
defaultAzureCredentialOptions.VisualStudioCodeTenantId = tenant;
155-
defaultAzureCredentialOptions.VisualStudioTenantId = tenant;
156-
}
157160
AccessToken accessToken = await new DefaultAzureCredential(defaultAzureCredentialOptions).GetTokenAsync(tokenRequestContext, cts.Token);
158161
SqlClientEventSource.Log.TryTraceEvent("AcquireTokenAsync | Acquired access token for Default auth mode. Expiry Time: {0}", accessToken.ExpiresOn);
159162
return new SqlAuthenticationToken(accessToken.Token, accessToken.ExpiresOn);
160163
}
161164

162-
TokenCredentialOptions tokenCredentialOptions = new TokenCredentialOptions() { AuthorityHost = new Uri(authority) };
165+
TokenCredentialOptions tokenCredentialOptions = new TokenCredentialOptions() { AuthorityHost = new Uri(parameters.Authority) };
163166

164167
if (parameters.AuthenticationMethod == SqlAuthenticationMethod.ActiveDirectoryManagedIdentity || parameters.AuthenticationMethod == SqlAuthenticationMethod.ActiveDirectoryMSI)
165168
{
@@ -171,18 +174,9 @@ public override async Task<SqlAuthenticationToken> AcquireTokenAsync(SqlAuthenti
171174
AuthenticationResult result;
172175
if (parameters.AuthenticationMethod == SqlAuthenticationMethod.ActiveDirectoryServicePrincipal)
173176
{
174-
// We continue to use IConfidentialClientApplication as "Azure.Identity.ClientSecretCredential" requires "TenantId"
175-
// which may not be available when Authority Url does not contain "<tenantId>".
176-
IConfidentialClientApplication ccApp = ConfidentialClientApplicationBuilder.Create(parameters.UserId)
177-
.WithAuthority(parameters.Authority)
178-
.WithClientSecret(parameters.Password)
179-
.WithClientName(Common.DbConnectionStringDefaults.ApplicationName)
180-
.WithClientVersion(Common.ADP.GetAssemblyVersion().ToString())
181-
.Build();
182-
183-
result = ccApp.AcquireTokenForClient(scopes).ExecuteAsync().Result;
184-
SqlClientEventSource.Log.TryTraceEvent("AcquireTokenAsync | Acquired access token for Active Directory Service Principal auth mode. Expiry Time: {0}", result.ExpiresOn);
185-
return new SqlAuthenticationToken(result.AccessToken, result.ExpiresOn);
177+
AccessToken accessToken = await new ClientSecretCredential(audience, parameters.UserId, parameters.Password, tokenCredentialOptions).GetTokenAsync(tokenRequestContext, cts.Token);
178+
SqlClientEventSource.Log.TryTraceEvent("AcquireTokenAsync | Acquired access token for Active Directory Service Principal auth mode. Expiry Time: {0}", accessToken.ExpiresOn);
179+
return new SqlAuthenticationToken(accessToken.Token, accessToken.ExpiresOn);
186180
}
187181

188182
/*

0 commit comments

Comments
 (0)