Example #1
0
 public MainWindow()
 {
     InitializeComponent();
     _app = PublicClientApplicationBuilder.Create(ClientId)
            .WithAuthority(Authority)
            .WithDefaultRedirectUri()
            .Build();
     TokenCacheHelper.EnableSerialization(_app.UserTokenCache);
 }
        public MainWindow()
        {
            InitializeComponent();
            _app = PublicClientApplicationBuilder.Create(clientId)
                   .WithAdfsAuthority(authority, false).WithRedirectUri(redirectUri).Build();


            TokenCacheHelper.EnableSerialization(_app.UserTokenCache);
            GetTodoList();
        }
        public MainWindow()
        {
            InitializeComponent();
            _app = PublicClientApplicationBuilder.Create(ClientId)
                   .WithAuthority(Authority)
                   .WithRedirectUri("http://localhost") // needed only for the system browser
                   .Build();

            TokenCacheHelper.EnableSerialization(_app.UserTokenCache);
            GetTodoList();
        }
Example #4
0
        protected override async void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);

            Scopes = new string[] { todoListServiceScope };

            // Initialize the PublicClientApplication
            app = PublicClientApplicationBuilder.Create(clientId)
                  .Build();

            TokenCacheHelper.EnableSerialization(app.UserTokenCache);


            // TODO: Check if the user is already signed in.
            // As the app starts, we want to check to see if the user is already signed in.
            // You can do so by trying to get a token from MSAL, using the method
            // AcquireTokenSilent.  This forces MSAL to throw an exception if it cannot
            // get a token for the user without showing a UI.
            try
            {
                var accounts = await app.GetAccountsAsync();

                var result = await app.AcquireTokenSilent(Scopes, accounts.FirstOrDefault())
                             .ExecuteAsync();

                // If we got here, a valid token is in the cache - or MSAL was able to get a new oen via refresh token.
                // Proceed to fetch the user's tasks from the TodoListService via the GetTodoList() method.

                SignInButton.Content = "Clear Cache";
                GetTodoList();
            }
            catch (MsalUiRequiredException)
            {
                // The app should take no action and simply show the user the sign in button.
            }
            catch (MsalException ex)
            {
                if (ex.ErrorCode == "failed_to_acquire_token_silently")
                {
                    // If user interaction is required, the app should take no action,
                    // and simply show the user the sign in button.
                }
                else
                {
                    // Here, we catch all other MsalExceptions
                    string message = ex.Message;
                    if (ex.InnerException != null)
                    {
                        message += "Inner Exception : " + ex.InnerException.Message;
                    }
                    MessageBox.Show(message);
                }
            }
        }
Example #5
0
        public MainWindow()
        {
            InitializeComponent();

            //Create an instance of PublicClientApplication using the Build Patten
            _app = PublicClientApplicationBuilder.Create(clientId)
                   .WithAuthority(authority)
                   .Build();

            //Hooking our file cache into the UserTokenCache
            TokenCacheHelper.EnableSerialization(_app.UserTokenCache);

            //Retrieve the existing data from the database
            GetTodoList();
        }
        static async Task Main(string[] args)
        {
            // Create the public client application (desktop app), with a default redirect URI
            // for these. Enable PoP
            var app = PublicClientApplicationBuilder.Create(ClientId)
                      .WithAuthority(Authority)
                      .WithDefaultRedirectUri()
                      .WithExperimentalFeatures() // Needed for PoP
                      .Build();

            // Enable a simple token cache serialiation so that the user does not need to
            // re-sign-in each time the application is run
            TokenCacheHelper.EnableSerialization(app.UserTokenCache);

            // Add an item to the list maintained in the Web API, and then list the items in the list
            HttpClient httpClient = new HttpClient();

            while (true)
            {
                await AddItemToList(app, httpClient);
                await WriteItemsInList(app, httpClient);
            }
        }
 public MainWindow()
 {
     InitializeComponent();
     app = new PublicClientApplication(clientId, authority, TokenCacheHelper.GetUserCache());
     GetTodoList();
 }
        protected override async void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);

            Scopes = new string[] { todoListServiceScope };

            // Initialize the PublicClientApplication
            app = new PublicClientApplication(clientId, "https://login.microsoftonline.com/common/v2.0", TokenCacheHelper.GetUserCache());
            AuthenticationResult result = null;

            // TODO: Check if the user is already signed in.
            // As the app starts, we want to check to see if the user is already signed in.
            // You can do so by trying to get a token from MSAL, using the method
            // AcquireTokenSilent.  This forces MSAL to throw an exception if it cannot
            // get a token for the user without showing a UI.
            try
            {
                var accounts = await app.GetAccountsAsync();

                result = await app.AcquireTokenSilentAsync(Scopes, accounts.FirstOrDefault());

                // If we got here, a valid token is in the cache - or MSAL was able to get a new oen via refresh token.
                // Proceed to fetch the user's tasks from the TodoListService via the GetTodoList() method.

                SignInButton.Content = "Clear Cache";
                GetTodoList();
            }
            catch (MsalUiRequiredException)
            {
                // The app should take no action and simply show the user the sign in button.
            }
            catch (MsalException ex)
            {
                if (ex.ErrorCode == "failed_to_acquire_token_silently")
                {
                    // If user interaction is required, the app should take no action,
                    // and simply show the user the sign in button.
                }
                else
                {
                    // Here, we catch all other MsalExceptions
                    DisplayInnerException(ex);
                }
            }
        }