Пример #1
0
        // References:
        // http://msdn.microsoft.com/en-us/library/aa480470.aspx
        /// <summary>
        /// Accepts a user credentials if they haven't been saved or if they have changed, adds them to the credential cache, and makes
        /// a call to EWS to verify that the credentials work. If the credentials work, then a confirmation is sent to the credential
        /// manager and the credentials are provided back to the application to be used to make EWS calls.
        /// </summary>
        internal static void AppLogin(ref ExchangeService service)
        {
            try
            {
                // Create the credential prompt that will handle the user credentials.
                CredPrompt prompt = new CredPrompt("Exchange Web Services");
                DialogResult result;

                // Show the prompt. This will either prompt for the user credentials or, if they
                // already exist on the system, it will load credentials from the cache in the CredDialog object.
                result = prompt.ShowPrompt();
                if (result == DialogResult.OK)
                {
                    // Authenticate the credentials and if they work, confirm the credentials and provide the
                    // ExchangeService object back to the application.
                    if (Authenticate(prompt.Name, prompt.Password, ref service))
                    {
                        // The credentials were authenticated. Confirm that we want the
                        // credential manager to save our credentials.
                        if (prompt.SaveChecked)
                            prompt.Confirm(true);
                    }

                    // The credentials were not authenticated.
                    else
                    {
                        try
                        {
                            prompt.Confirm(false);
                        }

                        catch (ApplicationException)
                        {
                            Console.Write("Would you like to retry connecting to EWS? [Y/N]: ");

                            while (true)
                            {
                                ConsoleKeyInfo userInput = Console.ReadKey();
                                Console.WriteLine();
                                if (userInput.Key == ConsoleKey.Y)
                                    AppLogin(ref service);
                                else if (userInput.Key == ConsoleKey.N)
                                {
                                    Environment.Exit(0);
                                }
                                else
                                    Console.WriteLine("Please press 'Y' or 'N'");
                            }
                        }
                    }
                }

                else if (result != DialogResult.Cancel)
                {
                    throw new ApplicationException("Unhandled condition.");
                }
            }

            // This will capture errors stemming from CredUICmdLinePromptForCredentials.
            catch(ApplicationException e)
            {
                Console.WriteLine("Error: {0}", e.Message);
            }
        }
Пример #2
0
        // References:
        // http://msdn.microsoft.com/en-us/library/aa480470.aspx

        /// <summary>
        /// Accepts a user credentials if they haven't been saved or if they have changed, adds them to the credential cache, and makes
        /// a call to EWS to verify that the credentials work. If the credentials work, then a confirmation is sent to the credential
        /// manager and the credentials are provided back to the application to be used to make EWS calls.
        /// </summary>
        internal static void AppLogin(ref ExchangeService service)
        {
            try
            {
                // Create the credential prompt that will handle the user credentials.
                CredPrompt   prompt = new CredPrompt("Exchange Web Services");
                DialogResult result;

                // Show the prompt. This will either prompt for the user credentials or, if they
                // already exist on the system, it will load credentials from the cache in the CredDialog object.
                result = prompt.ShowPrompt();
                if (result == DialogResult.OK)
                {
                    // Authenticate the credentials and if they work, confirm the credentials and provide the
                    // ExchangeService object back to the application.
                    if (Authenticate(prompt.Name, prompt.Password, ref service))
                    {
                        // The credentials were authenticated. Confirm that we want the
                        // credential manager to save our credentials.
                        if (prompt.SaveChecked)
                        {
                            prompt.Confirm(true);
                        }
                    }

                    // The credentials were not authenticated.
                    else
                    {
                        try
                        {
                            prompt.Confirm(false);
                        }

                        catch (ApplicationException)
                        {
                            Console.Write("Would you like to retry connecting to EWS? [Y/N]: ");

                            while (true)
                            {
                                ConsoleKeyInfo userInput = Console.ReadKey();
                                Console.WriteLine();
                                if (userInput.Key == ConsoleKey.Y)
                                {
                                    AppLogin(ref service);
                                }
                                else if (userInput.Key == ConsoleKey.N)
                                {
                                    Environment.Exit(0);
                                }
                                else
                                {
                                    Console.WriteLine("Please press 'Y' or 'N'");
                                }
                            }
                        }
                    }
                }

                else if (result != DialogResult.Cancel)
                {
                    throw new ApplicationException("Unhandled condition.");
                }
            }

            // This will capture errors stemming from CredUICmdLinePromptForCredentials.
            catch (ApplicationException e)
            {
                Console.WriteLine("Error: {0}", e.Message);
            }
        }