示例#1
0
        public int AnonymousUserInterface(ref IWorkgroupService workgroupProxy, IRegistrationService registrationProxy, ConnectionHandler connectionHandler, ClientAccount clientAccount)
        {
            User user = new User();
            bool operationSuccess;
            int  choice = 0;
            bool ok     = false;

            Console.WriteLine("---AUTHENTICATION SERVICE---\n");
            Console.WriteLine("1. Login");
            Console.WriteLine("2. Register");

            do
            {
                Console.Write("Choice: ");

                if (!Int32.TryParse(Console.ReadLine(), out choice))
                {
                    Console.WriteLine("Choice is a number!");
                }
                ok = true;
            } while (ok != true);

            switch (choice)
            {
            case 1:
            {
                string   answerOne        = null;
                string   answerTwo        = null;
                LoginDTO loginInformation = new LoginDTO();

                InputCredentials(clientAccount);
                if (clientAccount.RequireSafeLogin)
                {
                    Console.WriteLine("Server demands safe login. Input answers to security questions in order to log in");
                    Console.WriteLine(clientAccount.SecurityQuestionOne);
                    answerOne = Console.ReadLine();
                    Console.WriteLine(clientAccount.SecurityQuestionTwo);
                    answerTwo = Console.ReadLine();
                }

                if (connectionHandler.workgroupChannel.Credentials.UserName.UserName != null)        //Reopen channel for new credentials
                {
                    if (connectionHandler.CloseWorkgroupChannel())
                    {
                        connectionHandler.OpenWorkgroupChannel();
                        connectionHandler.workgroupChannel.Credentials.UserName.UserName = clientAccount.Username;
                        connectionHandler.workgroupChannel.Credentials.UserName.Password = clientAccount.Password;
                        workgroupProxy = connectionHandler.workgroupChannel.CreateChannel();
                    }
                }
                else
                {
                    connectionHandler.workgroupChannel.Credentials.UserName.UserName = clientAccount.Username;
                    connectionHandler.workgroupChannel.Credentials.UserName.Password = clientAccount.Password;
                    workgroupProxy = connectionHandler.workgroupChannel.CreateChannel();
                }

                try
                {
                    loginInformation = workgroupProxy.Login(clientAccount.Username, clientAccount.Password, answerOne, answerTwo);
                }

                /*catch(ArgumentException e)
                 * {
                 *  Console.WriteLine(e.Message);
                 * }
                 * catch(FaultException fe)
                 * {
                 *  Console.WriteLine(fe.Message, fe.Reason);
                 * }*/
                catch (Exception e)
                {
                    if (e is FaultException)
                    {
                        Console.WriteLine(e.Message);
                    }
                    if (e is ArgumentException)
                    {
                        Console.WriteLine(e.Message);
                    }
                }

                if (loginInformation.Authenticated)
                {
                    clientAccount.Authenticated = true;
                    clientAccount.ClientGroup   = loginInformation.UserGroup;
                }
                else
                {
                    if (loginInformation.SecurityQuestionOne != null)
                    {
                        clientAccount.RequireSafeLogin    = true;
                        clientAccount.SecurityQuestionOne = loginInformation.SecurityQuestionOne;
                        clientAccount.SecurityQuestionTwo = loginInformation.SecurityQuestionTwo;
                    }

                    Console.WriteLine("Failed to log in");
                }

                break;
            }

            case 2:
            {
                User newUserAccount = InputUserInfo();
                try
                {
                    operationSuccess = registrationProxy.Register(newUserAccount);
                    if (operationSuccess)
                    {
                        Console.WriteLine("Successfully registered a new account!");
                    }
                }
                catch (FaultException fe)
                {
                    Console.WriteLine(fe.Message);
                    Console.WriteLine(fe.InnerException);
                }
                break;
            }

            case 0:
                break;
            }
            return(choice);
        }