示例#1
0
        private bool action_register()
        {
            UserId = "";
            string temp;

            while (UserId == "")
            {
                _chatserver.Write(_client.GetStream(),
                                  AuthenticationProtocolValues.UseridPrompt);
                temp = _chatserver.Read(_client.GetStream());
                if (!ChatUser.ValidUserId(temp))
                {
                    _chatserver.Write(_client.GetStream(),
                                      AuthenticationProtocolValues.UseridStartWithWordcharMsg);
                    continue;
                }
                if (IsRegisteredUser(temp))
                {
                    _chatserver.Write(_client.GetStream(),
                                      AuthenticationProtocolValues.UserAlreadyRegisteredMsg);
                }
                else
                {
                    UserId = temp;
                }
            }
            Password = "";
            while (Password == "")
            {
                _chatserver.Write(_client.GetStream(),
                                  AuthenticationProtocolValues.PasswordPrompt);
                temp = _chatserver.Read(_client.GetStream());
                if (!ChatUser.ValidPassword(temp))
                {
                    _chatserver.Write(_client.GetStream(),
                                      AuthenticationProtocolValues.PasswordAtLeast4CharMsg);
                }
                else
                {
                    Password = temp;
                }
            }
            _chatserver.AddChatUser(UserId, new ChatUser(UserId, Password));
            _chatserver.SerializeChatUsers("users.bin");

            return(true);
        }
示例#2
0
        //REGISTER
        private bool action_register()
        {
            //init user id to blank
            UserID = "";
            string temp = "";

            //Loop until user id not blank
            while (UserID == "")
            {
                //prompt for user id
                chatserver.Write(client.GetStream(),
                                 AuthenticationProtocolValues.USERID_PROMPT);
                temp = chatserver.Read(client.GetStream());

                //check validity of user id
                if (!ChatUser.ValidUserID(temp))
                {
                    chatserver.Write(client.GetStream(),
                                     AuthenticationProtocolValues.USERID_START_WITH_WORDCHAR_MSG);
                    continue;
                }

                //if the user id is already used
                if (IsRegisteredUser(temp))
                {
                    chatserver.Write(client.GetStream(),
                                     AuthenticationProtocolValues.USER_ALREADY_REGISTERED_MSG);

                    continue;
                }
                else
                {
                    UserID = temp;                   //valid and not used id
                }
            }

            //init password to blank
            Password = "";
            //Loop until password is not blank
            while (Password == "")
            {
                //prompt for password
                chatserver.Write(client.GetStream(),
                                 AuthenticationProtocolValues.PASSWORD_PROMPT);

                temp = chatserver.Read(client.GetStream());

                //check validity of password
                if (!ChatUser.ValidPassword(temp))
                {
                    chatserver.Write(client.GetStream(),
                                     AuthenticationProtocolValues.PASSWORD_AT_LEAST_4_CHAR_MSG);

                    continue;
                }
                else
                {
                    Password = temp;                  //Valid password is assigned
                }
            }

            //Add newly registered chat user
            chatserver.AddChatUser(UserID, new ChatUser(UserID, Password));
            //Serialized the chat users to file to reflect new user
            chatserver.SerializeChatUsers("users.bin");

            return(true);
        }