// Login to the user's account and create a UCWA app

        static void Login()
        {
            #region Login

            // Clear any cached tokens.
            // We do this to ensure logins with different accounts work
            // during the same launch of the app

            authenticationContext.TokenCache.Clear();

            Console.WriteLine("How do you want to login?");
            Console.WriteLine("console | dialog | code >");
            string loginStyle = Console.ReadLine();

            AuthenticationResult testCredentials = null;
            UserCredential       uc = null;

            switch (loginStyle.ToLower())
            {
            case "console":
                uc = GetUserCredentials();
                testCredentials = UcwaSfbo.AzureAdAuth.GetAzureAdToken(authenticationContext, sfboResourceAppId, clientId, redirectUri, uc);
                break;

            case "dialog":
                if (redirectUri == String.Empty)
                {
                    Console.WriteLine("You haven't defined redirectUri which is needed if you want to sign in with a dialog");
                    return;
                }
                testCredentials = UcwaSfbo.AzureAdAuth.GetAzureAdToken(authenticationContext, sfboResourceAppId, clientId, redirectUri, uc);
                break;

            case "code":
                DeviceCodeResult deviceCodeResult = authenticationContext.AcquireDeviceCodeAsync(sfboResourceAppId, clientId).Result;
                Console.WriteLine(deviceCodeResult.Message);
                Console.WriteLine("Or, use Control-C to exit the app");
                testCredentials = authenticationContext.AcquireTokenByDeviceCodeAsync(deviceCodeResult).Result;
                break;

            default:
                Console.Write("Please select a login style and try again");
                Console.Write("\n");
                return;
            }

            if (testCredentials == null)
            {
                Console.WriteLine("We encountered an Azure AD error");
                Console.WriteLine("Check your tenant, clientID, and credentials");
                return;
            }
            ucwaApplicationsUri = UcwaAutodiscovery.GetUcwaRootUri(authenticationContext, sfboResourceAppId, clientId, redirectUri, uc);

            Console.WriteLine("We'll store the base UCWA app URI for use with UCWA app calls");
            Console.WriteLine("We prefix this to the links returned from the UCWA apps POST");
            Console.WriteLine("Since these links aren't full URIs");
            ucwaApplicationsHost = Helpers.ReduceUriToProtoAndHost(ucwaApplicationsUri);
            Console.WriteLine("ucwaApplicationsHost is " + ucwaApplicationsHost);

            Console.WriteLine("Get a token to access the user's UCWA Applications Resources from Azure AD.");
            Console.WriteLine("We can re-use this token for each UCWA app call");
            ucwaAuthenticationResult = AzureAdAuth.GetAzureAdToken(authenticationContext, ucwaApplicationsHost, clientId, redirectUri, uc);

            Console.WriteLine("Now we'll create and/or query UCWA Apps via POST");
            Console.WriteLine("Well create a UCWA apps object to pass to CreateUcwaApps");

            UcwaApplications.UcwaMyAppsObject ucwaMyAppsObject = new UcwaApplications.UcwaMyAppsObject()
            {
                UserAgent  = "myAgent",
                EndpointId = "1234",
                Culture    = "en-US"
            };

            Console.WriteLine("Making request to ucwaApplicationsUri " + ucwaApplicationsUri);
            createUcwaAppsResults = UcwaApplications.CreateUcwaApps(ucwaAuthenticationResult, ucwaApplicationsUri, ucwaMyAppsObject);

            return;
        }
        static void Main(string[] args)
        {
            string commandString = string.Empty;

            Console.ForegroundColor = ConsoleColor.White;

            //Configure the app insights instrumentation key
            string instrumentationKey = ConfigurationManager.AppSettings["AppInsightsIKey"];

            TelemetryConfiguration.Active.InstrumentationKey = instrumentationKey;
            TelemetryClient tc = new TelemetryClient();

            try
            {
                //Set the UCWA applications host
                ConfigData.ucwaApplicationsHost = ucwaApplicationsHost;

                //Get the Authentication token
                ConfigData.authToken = LyncAuth.GetOAuthToken(httpClient, userName, password, domain, lyncOAuthUri, tc);
                if (ConfigData.authToken.access_token != null)
                {
                    //Create an application on the UCWA server
                    List <string> Modalities = new List <string>();
                    Modalities.Add("PhoneAudio");
                    Modalities.Add("Messaging");
                    //Set the properties for the applications resource
                    UcwaMyApps ucwaMyAppsObject = new UcwaMyApps()
                    {
                        UserAgent  = "IntranetBot",
                        EndpointId = Guid.NewGuid().ToString(),
                        Culture    = "en-US"
                    };
                    //Create an application on the Skype UCWA server and register it.
                    createUcwaAppsResults = UcwaApplications.CreateUcwaApps(httpClient, ConfigData.authToken, ucwaApplicationsUri, ucwaMyAppsObject, tc);

                    //Set IM status as online
                    SetIMStatus(tc);

                    //Get the message. The method "GetIM_Step03_Events" in the class UCWAReceiveMessage is a recursive function
                    //which will keep listening for the incoming messages
                    GetMessage(tc, true);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                tc.TrackException(ex);
            }

            while (!commandString.Equals("Exit", StringComparison.InvariantCultureIgnoreCase))
            {
                Console.WriteLine(Environment.NewLine);
                Console.WriteLine("Enter exit to exit");
                commandString = Console.ReadLine();
                //commandString = "msg";
                switch (commandString.ToUpper())
                {
                case "EXIT":
                    Console.WriteLine("Bye!");;
                    break;

                default:
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Invalid command.");
                    break;
                }
            }
        }