Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

[release/2.2] Added AAD Authentication using Access Token #31039

Merged
merged 5 commits into from
Aug 17, 2018

Conversation

AfsanehR-zz
Copy link
Contributor

@AfsanehR-zz AfsanehR-zz commented Jul 12, 2018

Fixes #13660
2.2 Servicing:

Description:
The desktop .NET Framework 4.6 and newer has an AccessToken property on the SqlConnection class which can be used to authenticate to an Azure SQL database using an access token issued by Azure AD. However, this property is not present on the version of SqlConnection provided in the System.Data.SqlClient NuGet package of CoreFX. The lack of the AccessToken property makes it difficult, if not impossible, to support modern directory-based auth scenarios for Azure SQL DB while also targeting .NET Standard.

Customer Impact:
The lack of a way to connect to an Azure SQL database in CoreFx keeps existing Azure SQL customers from adopting CoreFX. It also precludes customers who already use CoreFX from subscribing to Azure SQL services. This small change will enable customers to move forward with a CoreFx + Azure SQL combination.

The code is already in the master branch of the corefx repo.

Regression?
None. These changes are ported over from .NET Framework to .NET Core.

Risk:
Low. The code changes already existed on .NET Framework.

The same pr#30342 is already merged on master branch.

removed unused parts in code

removed unnecessary code

fix a comment

Review feedbacks from Stephen Toub + Added a verification test

addressed review feedbacks

use Debug.Fail instead of Debug.Assert
Copy link

@keeratsingh keeratsingh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving as this is a port of #30342 on Release 2.2 branch

@@ -34,6 +34,7 @@ public sealed partial class SqlConnection : DbConnection, ICloneable
private SqlCredential _credential;
private string _connectionString;
private int _connectRetryCount;
private string _accessToken; // Access Token to be used for token based authententication
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo in comment, authentication

// If a connection is connecting or is ever opened, AccessToken cannot be set
if (!InnerConnection.AllowSetConnectionString)
{
throw ADP.OpenConnectionPropertySet("AccessToken", InnerConnection.State);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possible nameof(AccessToken) ?

@AfsanehR-zz
Copy link
Contributor Author

@Petermarcu @stephentoub @danmosemsft Could you please review this pr? This is the same as #30342 targeting to 2.2 Release branch. I have modified the code changes @Wraith2 has suggested, will update the PR once I receive your feedback. Thanks!

{
return ADP.InvalidOperation(SR.GetString(SR.ADP_InvalidMixedUsageOfAccessTokenAndUserIDPassword));
}
static internal Exception InvalidMixedUsageOfCredentialAndAccessToken()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: it'd be good to consistently use "internal static" rather than sometimes using "static internal"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

{
return _accessToken;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can just be:

internal string AccessToken => _accessToken;

similar to the property right above it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@@ -74,6 +85,13 @@ private void CalculateHashCode()
_hashValue = _hashValue * 17 + _credential.GetHashCode();
}
}
else if (_accessToken != null)
{
unchecked
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the unchecked needed? Is checked arithmetic enabled for this project?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see unchecked being used in other parts of the SqlClient project.

@danmoseley
Copy link
Member

danmoseley commented Jul 24, 2018

@afsanehr let's ask @Petermarcu what process he wants here when he returns tomorrow.

@AfsanehR-zz AfsanehR-zz changed the base branch from release/2.2 to release/2.1 August 14, 2018 20:35
@AfsanehR-zz
Copy link
Contributor Author

AfsanehR-zz commented Aug 14, 2018

@dotnet-bot Test OSX x64 Debug Build please

@AfsanehR-zz AfsanehR-zz modified the milestones: 2.2, 2.1.x Aug 14, 2018
@AfsanehR-zz
Copy link
Contributor Author

@dotnet-bot Test OSX x64 Debug Build

@AfsanehR-zz
Copy link
Contributor Author

@danmosemsft Just confirming @Petermarcu approved merging this pr.

@AfsanehR-zz AfsanehR-zz added the Servicing-consider Issue for next servicing release review label Aug 15, 2018
@danmoseley
Copy link
Member

@afsanehr @keeratsingh I see this adds API (addition to ref*cs). I don't believe we can do that in servicing safely. Was this already discussed? If not, this shoudl go in 2.2 or 3.0.

Also if https://github.com/dotnet/corefx/issues/31082 depends on this the same would be true for that.

@AfsanehR-zz AfsanehR-zz changed the base branch from release/2.1 to release/2.2 August 16, 2018 16:36
@AfsanehR-zz AfsanehR-zz modified the milestones: 2.1.x, 2.2 Aug 16, 2018
keeratsingh pushed a commit to keeratsingh/corefx that referenced this pull request Aug 16, 2018
@karelz karelz changed the title Added AAD Authentication using Access Token [release/2.2] Added AAD Authentication using Access Token Aug 16, 2018
@keeratsingh keeratsingh merged commit 1ae377f into dotnet:release/2.2 Aug 17, 2018
@danmoseley danmoseley removed the Servicing-consider Issue for next servicing release review label Aug 21, 2018
@danmoseley
Copy link
Member

This is in the 2.2 branch but we did not have the package authoring correct to get it in the impending preview 1. We can follow up to get it in preview 2.

@weshaggard

@weshaggard
Copy link
Member

@afsanehr yes given that you are adding new APIs you should bump the minor version of the package as well as the assembly. You will also likely need to bump master to a higher version as well. You will also need to add the SqlClient package to the list of packages to build similar to #31891

@@ -1403,7 +1471,7 @@ public static void ChangePassword(string connectionString, SqlCredential credent
throw ADP.InvalidArgumentLength(nameof(newSecurePassword), TdsEnums.MAXLEN_NEWPASSWORD);
}

SqlConnectionPoolKey key = new SqlConnectionPoolKey(connectionString, credential);
SqlConnectionPoolKey key = new SqlConnectionPoolKey(connectionString, credential: null, accessToken: null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was changing credential to null here expected? There is another similar spot below on line 1512 as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/cc: @danmosemsft / @stephentoub for visibility

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 1512 has a bug and credential shouldn't be passed in as null, instead of the credential parameter should be passed through.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this line, i.e. 1474, its buggy. No null should be passed in for Credential

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants