public MainWindow()
 {
     InitializeComponent();
     app = new PublicClientApplication(clientId, authority, TokenCacheHelper.GetUserCache());
     GetTodoList();
 }
Example #2
0
        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
            {
                result = await app.AcquireTokenSilentAsync(Scopes, app.Users.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
                    string message = ex.Message;
                    if (ex.InnerException != null)
                    {
                        message += "Inner Exception : " + ex.InnerException.Message;
                    }
                    MessageBox.Show(message);
                }
            }
        }