控制台应用程序接受' /'进入密码,即使它不应该

时间:2016-10-14 07:45:28

标签: c#

我已在Console中创建了C#个应用程序。我不希望我的应用程序接受'/',但它继续接受它作为一个角色。我不明白我做错了什么。任何帮助将不胜感激。

using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace Student_Enrollment_System
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome To the Student Enrollment System Please Choose One Of The Below Opions");
            Console.WriteLine();

            Console.WriteLine("(1) Sign Up");
            Console.WriteLine("(2) Sign In");
            Console.WriteLine("(3) Update password");
            Console.WriteLine("(4) Recover password");
            Console.WriteLine("(5) Exit");

            int number = Convert.ToInt32(Console.ReadLine());
            switch (number)
            {
                case 1:
                    Console.WriteLine("type Student First Name");
                {
                    string filepath = "C:\\SES\\User.txt";
                    //StreamWriter filewr = new StreamWriter(@"C:\Users\G510\Documents\Student Enrollment System\User.txt", true);

                    string studentname = Convert.ToString(Console.ReadLine());

                    Console.WriteLine("Type in Student ID");
                    string student_id = Convert.ToString(Console.ReadLine());

                    Console.WriteLine("Type in Student PassWord");
                    string student_password = Convert.ToString(Console.ReadLine());

                    if (File.Exists(filepath))
                    {
                        Console.WriteLine("data has been added to the same file name");

                        StreamWriter filewr = new StreamWriter(filepath, true);

                        filewr.WriteLine(studentname);
                        filewr.WriteLine(student_id);

                        do
                        {
                        } while (student_password.Contains('/') == true);

                        MessageBox.Show("forward slash is not allowed ");

                        filewr.WriteLine(student_password);
                        filewr.Flush();
                        filewr.Close();
                    }

                    else if (!File.Exists(filepath))
                    {
                        StreamWriter filewr = new StreamWriter(filepath);

                        filewr.WriteLine(studentname);
                        filewr.WriteLine(student_id);

                        do
                        {
                        } while (student_password.Contains('/') == true);

                        MessageBox.Show("forward slash is not allowed ");

                        filewr.WriteLine(student_password);
                        filewr.Flush();
                        filewr.Close();
                    }
                    return;

                    Console.WriteLine("Thanks For Signing Up Press Any Key To Exit");
                    Console.ReadKey();
                }
                    break;

                case 2:
                    Console.WriteLine("(2) Sign In");
                    break;

                case 3:
                    Console.WriteLine("(3) Update password");
                    break;

                case 4:
                    Console.WriteLine("(4) Recover password");
                    break;

                case 5:
                    Console.WriteLine("(5) Exit");
                    break;
            }
        }
    }

}

3 个答案:

答案 0 :(得分:1)

你应该改变

string student_password = Console.ReadLine();
if (student_password.Contains('/') == true)
{ 
    MessageBox.Show("/ is not allowed"); 
}

string student_password = Console.ReadLine();
while (student_password.Contains('/') == true)
{
    MessageBox.Show("/ is not allowed");
    student_password = Console.ReadLine();
}

现在,只要student_password包含斜杠,系统就会要求您输入新密码。

答案 1 :(得分:1)

您的代码中存在一些问题:

首先,您不应该在控制台应用程序中使用MessageBox.Show,它们是为WinForm应用程序设计的。它不能保证在所有环境中都能正常工作。

其次,如果你发现一个不好的角色,你什么也不做,试试类似:

  if(student_password.Contains('/') == true) 
  { 
      Console.WriteLine("Sorry, but passwords can't contain '/'". Please try again");
      Console.Write("Password: ");
      student_password = Console.ReadLine();
  }

您可以再试一次,但您需要将密码位移动到新功能中。或者只是中止:

  if(student_password.Contains('/') == true) 
  { 
      Console.WriteLine("Sorry, but passwords can't contain '/'". Aborting");
      return;
  }

最后,我会认真考虑为什么用户的密码不能有/。这是一个特殊的角色,它将有助于安全。一般来说,为了您的利益,不要使密码限制较弱。

答案 2 :(得分:-1)

你没有对" /"做出反应炭。

尝试这样的事情:

MessageBox.Show(" / is not allowed. Aborting ");
return;

student_password.Replace('/', '')