Skip to content

Commit

Permalink
Merge pull request #16 from Azure-Samples/fix-adal-usage
Browse files Browse the repository at this point in the history
Fix ADAL usage to use MSA
  • Loading branch information
yeskarthik committed Jun 18, 2020
2 parents 9aff6ca + 2a2547c commit 8110cbd
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions csharp-tsi-ga-sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,33 +169,32 @@ public static async Task SampleAsync()

private static async Task<string> AcquireAccessTokenAsync()
{
if (ApplicationClientId == "#DUMMY#" || ApplicationClientSecret == "#DUMMY#" || Tenant.StartsWith("#DUMMY#"))
if (AadClientApplicationId == "#PLACEHOLDER#" || AadScopes.Length == 0 || AadRedirectUri == "#PLACEHOLDER#" || AadTenantName.StartsWith("#PLACEHOLDER#"))
{
throw new Exception(
$"Use the link {"https://docs.microsoft.com/en-us/azure/time-series-insights/time-series-insights-authentication-and-authorization"} to update the values of 'ApplicationClientId', 'ApplicationClientSecret' and 'Tenant'.");
throw new Exception($"Use the link {"https://docs.microsoft.com/azure/time-series-insights/time-series-insights-get-started"} to update the values of 'AadClientApplicationId', 'AadScopes', 'AadRedirectUri', and 'AadAuthenticationAuthority'.");
}

var authenticationContext = new AuthenticationContext(
$"https://login.windows.net/{Tenant}",
TokenCache.DefaultShared);
IPublicClientApplication app = PublicClientApplicationBuilder
.Create(AadClientApplicationId)
.WithRedirectUri(AadRedirectUri)
.WithAuthority(AadAuthenticationAuthority)
.Build();

AuthenticationResult token = await authenticationContext.AcquireTokenAsync(
resource: "https://api.timeseries.azure.com/",
clientCredential: new ClientCredential(
clientId: ApplicationClientId,
clientSecret: ApplicationClientSecret));
AuthenticationResult result = await app
.AcquireTokenInteractive(AadScopes)
.ExecuteAsync();

// Show interactive logon dialog to acquire token on behalf of the user.
// Suitable for native apps, and not on server-side of a web application.
//AuthenticationResult token = await authenticationContext.AcquireTokenAsync(
// resource: "https://api.timeseries.azure.com/",
// clientId: {your clientId generated from the steps above},
// AuthenticationResult result = await app
// .AcquireTokenInteractive(AadScopes)
// .ExecuteAsync();
//
// // Set redirect URI for Azure PowerShell
// redirectUri: new Uri("urn:ietf:wg:oauth:2.0:oob"),
// parameters: new PlatformParameters(PromptBehavior.Auto));
return token.AccessToken;

return result.AccessToken;
}

private static HttpWebRequest CreateHttpsWebRequest(string host, string method, string path, string accessToken, string[] queryArgs = null)
Expand Down

0 comments on commit 8110cbd

Please sign in to comment.