示例#1
0
        //Main authentication method
        public bool Authenticate()
        {
            byte[] bytes            = new byte[256];
            string data             = null;
            bool   validate_command = false;

            //Get user command
            //Valid command is login and register
            while (!validate_command)
            {
                chatserver.Write(client.GetStream(),
                                 AuthenticationProtocolValues.COMMAND_PROMPT);
                data = chatserver.Read(client.GetStream());

                data = data.Trim();

                //default action is to assume that the command is invalid
                action = new AuthenticateAction(action_default);

                //actions are reassigned based on data entered by user
                if (data.ToUpper().IndexOf(AuthenticationProtocolValues.LOGIN_COMMAND) == 0)
                {
                    action           = new AuthenticateAction(action_login);
                    validate_command = true;
                }

                if (data.ToUpper().IndexOf(AuthenticationProtocolValues.REGISTER_COMMAND) == 0)
                {
                    action           = new AuthenticateAction(action_register);
                    validate_command = true;
                }

                //perform the action
                //if it failed the the process has failed
                if (!action())
                {
                    return(false);
                }
            }            //While

            //when out of loop the command has succeed
            //send the authenticated message to the client
            chatserver.Write(client.GetStream(),
                             AuthenticationProtocolValues.AUTENTICATED_MSG);

            return(true);
        }
示例#2
0
 private void action_default()
 {
     _chatserver.Write(_client.GetStream(),
                       ChatProtocolValues.UNKNOWN_CMD_MSG(_readdata));
 }
示例#3
0
        public bool Authenticate()
        {
            var validateCommand = false;

            while (!validateCommand)
            {
                _chatserver.Write(_client.GetStream(),
                                  AuthenticationProtocolValues.CommandPrompt);
                var data = _chatserver.Read(_client.GetStream());
                data    = data.Trim();
                _action = action_default;
                if (data.ToUpper().IndexOf(AuthenticationProtocolValues.LoginCommand, StringComparison.Ordinal) == 0)
                {
                    _action         = action_login;
                    validateCommand = true;
                }

                if (data.ToUpper().IndexOf(AuthenticationProtocolValues.RegisterCommand, StringComparison.Ordinal) == 0)
                {
                    _action         = action_register;
                    validateCommand = true;
                }
                if (!_action())
                {
                    return(false);
                }
            }
            _chatserver.Write(_client.GetStream(),
                              AuthenticationProtocolValues.AutenticatedMsg);

            return(true);
        }