Esempio n. 1
0
        public static int Main(string[] args)
        {
            var publicClientApp = PublicClientApplicationBuilder.Create(ClientId)
                                  .WithB2CAuthority(AuthoritySignUpSignIn)
                                  .WithRedirectUri(RedirectUri)
                                  .WithLogging(Log, LogLevel.Info, false) // don't log PII details on a regular basis
                                  .Build();

            TokenCacheHelper.Bind(publicClientApp.UserTokenCache);
            AuthenticationResult authResult = null;

            try
            {
                var accounts = publicClientApp.GetAccountsAsync(PolicySignUpSignIn).GetAwaiter().GetResult();

                authResult = publicClientApp.AcquireTokenSilent(ApiScopes, accounts.FirstOrDefault())
                             .ExecuteAsync().GetAwaiter().GetResult();

                DisplayUserInfo(authResult);
            }
            catch (MsalUiRequiredException)
            {
                Console.WriteLine("You need to sign-in first");
                try
                {
                    authResult = Helpers.SignIn(publicClientApp, ApiScopes, AuthorityResetPassword, null).GetAwaiter().GetResult();
                    DisplayUserInfo(authResult);
                }
                catch (Exception e)
                {
                    return(ExitWithError(e.ToString()));
                }
            }
            catch (Exception ex)
            {
                return(ExitWithError($"Error Acquiring Token Silently:{Environment.NewLine}{ex}"));
            }

            Console.WriteLine($"Calling {ApiEndpoint}...");
            var resultText   = Helpers.GetHttpContentWithToken(ApiEndpoint, authResult.AccessToken).GetAwaiter().GetResult();
            var searchResult = JsonConvert.DeserializeObject <SearchResultContract>(resultText);

            Console.WriteLine("Result received");
            Console.WriteLine($"Number of classifications found: {searchResult.NumberOfClassificationsFound}");
            if (searchResult.NumberOfClassificationsFound > 0)
            {
                // Just printing first item from result list for demo purposes
                var firstDomain         = searchResult.Domains[0];
                var firstClassification = firstDomain.Classifications[0];
                Console.WriteLine($"First item in result list, domain: {firstDomain.Name}, classification: {firstClassification.Name}");
            }

            Console.WriteLine();
            Console.WriteLine("Press Enter to close");
            Console.ReadLine();
            return(0);
        }
Esempio n. 2
0
        public static int Main(string[] args)
        {
            publicClientApp = PublicClientApplicationBuilder.Create(ClientId)
                              .WithB2CAuthority(AuthoritySignUpSignIn)
                              .WithRedirectUri(RedirectUri)
                              .WithLogging(Log, LogLevel.Info, false) // don't log PII details on a regular basis
                              .Build();

            TokenCacheHelper.Bind(publicClientApp.UserTokenCache);

            Console.WriteLine("Press L to clear the token cache before continuing. Any other character just continues.");
            var keyInfo = Console.ReadKey();

            if (keyInfo.KeyChar == 'l' || keyInfo.KeyChar == 'L')
            {
                Console.WriteLine();
                Console.WriteLine("Clearing token cache...");
                ClearTokenCache().GetAwaiter().GetResult();
            }

            Console.WriteLine("Reading data...");
            if (SecuredExample(ApiEndpointSecure, out var resultText, out var exitWithError))
            {
                return(exitWithError);
            }

            var searchResult = JsonConvert.DeserializeObject <SearchResultContract>(resultText);

            Console.WriteLine("Result received");
            Console.WriteLine($"Number of classifications found: {searchResult.NumberOfClassificationsFound}");
            if (searchResult.NumberOfClassificationsFound > 0)
            {
                // Just printing first item from result list for demo purposes
                var firstDomain         = searchResult.Domains[0];
                var firstClassification = firstDomain.Classifications[0];
                Console.WriteLine($"First item in result list, domain: {firstDomain.Name}, classification: {firstClassification.Name}");
            }

            Console.WriteLine();
            Console.WriteLine("Press Enter to close");
            Console.ReadLine();
            return(0);
        }