From c88ddfeb0b6b6360e1beafd7674e4b9986c485f2 Mon Sep 17 00:00:00 2001 From: Jayendran Arumugam <25414541+jayendranarumugam@users.noreply.github.com> Date: Thu, 15 Aug 2019 10:38:32 +0530 Subject: [PATCH] updated program.cs Removed the redirect URL since its no longer needed, added the TLS 1.2 protocol. When I try to answer to the below user I thought it would be great to fix the original code https://community.powerbi.com/t5/Developer/Uploading-PBIX-File-in-C-sample-HELP/m-p/766766 --- PbixInstallerForPowerBI/Program.cs | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/PbixInstallerForPowerBI/Program.cs b/PbixInstallerForPowerBI/Program.cs index a3379a6..bb13968 100644 --- a/PbixInstallerForPowerBI/Program.cs +++ b/PbixInstallerForPowerBI/Program.cs @@ -16,9 +16,6 @@ class ProgramConstants { // update client id to reference an application registred with Azure public const string ClientID = "[[YOUR_CLIENT_ID_HERE]]"; - // Redirect URL needs to match reply URL in Azure registration - public const string RedirectUri = "https://localhost/PbixInstallerForPowerBI"; - // URLs for working with the Power BI REST API public const string AzureAuthorizationEndpoint = "https://login.microsoftonline.com/common"; public const string PowerBiServiceResourceUri = "https://analysis.windows.net/powerbi/api"; @@ -46,22 +43,17 @@ protected static void AcquireAccessToken() { // create new ADAL authentication context var authenticationContext = - new AuthenticationContext(ProgramConstants.AzureAuthorizationEndpoint); - - //use authentication context to trigger user login and then acquire access token - var userAuthnResult = - authenticationContext.AcquireTokenAsync(ProgramConstants.PowerBiServiceResourceUri, - ProgramConstants.ClientID, - new Uri(ProgramConstants.RedirectUri), - new PlatformParameters(PromptBehavior.Auto)).Result; + new AuthenticationContext(ProgramConstants.AzureAuthorizationEndpoint); + // To Enforce the TLS 1.2 Protocol + ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol | SecurityProtocolType.Tls12; + + // use authentication context to trigger user sign-in and return access token + var userCreds = new UserPasswordCredential("UserName@MyTenent.onMicrosoft.com", "myEasyToCrackPassword"); - //// use authentication context to trigger user sign-in and return access token - //var userCreds = new UserPasswordCredential("UserName@MyTenent.onMicrosoft.com", "myEasyToCrackPassword"); - - //var userAuthnResult = authenticationContext.AcquireTokenAsync(ProgramConstants.PowerBiServiceResourceUri, - // ProgramConstants.ClientID, - // userCreds).Result; + var userAuthnResult = authenticationContext.AcquireTokenAsync(ProgramConstants.PowerBiServiceResourceUri, + ProgramConstants.ClientID, + userCreds).Result; // cache access token in AccessToken field