我需要C#Console中的帐户帮助 [英] I need help with accounts in C# Console

查看:146
本文介绍了我需要C#Console中的帐户帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我会粘贴代码,然后解释我需要的内容。

  public   void  login()
{
sign:
Console.Write( \ n请输入您的用户名(例如用户 - 默认用户名):);
var username = Console.ReadLine();
Console.Write( \ n请输入您的密码(例如1111 - 默认用户密码): );
var password = Console.ReadLine();
if (username == rojus
如果(密码== dev
{
rojusbool = true ;
InSys();
}
如果(用户名== user
if (password == 1111
{
userbool = true ;
InSysuser();
}

if (用户名!= rojus
if (用户名!= user
{
Console.Write( \ n无效的用户名);
goto sign;
}
如果(密码!= dev
if (密码!= 1111
{
Console.Write( \\\
Invalid password
);
goto sign;
}

}

public void InSys()
{
if (rojusbool == true
{
Console.Write( / TOS / Rojus @:);
string help;
help = Console.ReadLine();
if (help == help
Console.WriteLine( \ nHelp - 显示命令列表\ nnlogout - 注销\\\
whofami - 告诉你是谁? - 关闭\\\
colorsettings - 更改文字颜色或文字高亮颜色。
);
if (help == logout
{
rojusbool = false ;
login();
}
如果(help == whoami
Console.WriteLine( \ n你是Rojus Lapinskas,一个C#程序员);
if (help == sd
shutdown();
if (help == colorsettings
Colorpack();
if (help!= help
if (help!= logout
if (help!= whoami
if (help!= sd
if (help!= colorsettings
Console.WriteLine( 命令无法识别);
}


}
public void InSysuser()
{
if (userbool == true
{

Console.Write( / TOS / User @:);
string help;
help = Console.ReadLine();
if (help == help
Console.WriteLine( \ nHelp - 显示命令列表\ nnlogout - 注销\\\
whofami - 告诉你是谁? - 关闭\\\
colorsettings - 更改文字颜色或文字高亮颜色。
);
if (help == logout
{
userbool = false ;
login();
}
如果(help == whoami
Console.WriteLine( \ n您是用户,具有基本权利)的用户;
if (help == sd
shutdown();
if (help == colorsettings
Colorpack();
if (help!= help
if (help!= logout
if (help!= whoami
if (help!= sd
if (help!= colorsettings
Console.WriteLine( 命令无法识别);
}
}

public void shutdown( )
{
Console.Clear();
Console.Write( \ n);
Console.Write( \ n按下进入和关闭计算机或虚拟机\ n< /跨度>);
Console.ReadLine();
while 1 == 1
{
Console.Clear();
}
}
public void Colorpack()
{
Console.Write( \ n输入文字颜色:);
var color = Console.ReadLine();
if (color == red
Console.ForegroundColor = ConsoleColor.Red;
if (color == orange
Console.ForegroundColor = ConsoleColor.Cyan;
if (color == yellow
Console.ForegroundColor = ConsoleColor.Yellow;
if (color == green
Console.ForegroundColor = ConsoleColor.Green;
if (color == blue
Console.ForegroundColor = ConsoleColor.Blue;
if (color == purple
Console.ForegroundColor = ConsoleColor.DarkMagenta;
if (color == black
Console.ForegroundColor = ConsoleColor.Black;
if (color == white
Console.ForegroundColor = ConsoleColor.White;
if (color == cyan
Console.ForegroundColor = ConsoleColor.Cyan;
if (color == darkblue
Console.ForegroundColor = ConsoleColor.DarkBlue;

// 设置背景颜色
Console.Write( 输入文本的背景颜色:);
var hcolor = Console.ReadLine();

if (hcolor == red
Console.BackgroundColor = ConsoleColor.Red;
if (hcolor == orange
Console.BackgroundColor = ConsoleColor.Cyan;
if (hcolor == yellow
Console.BackgroundColor = ConsoleColor.Yellow;
if (hcolor == green
Console.BackgroundColor = ConsoleColor.Green;
if (hcolor == blue
Console.BackgroundColor = ConsoleColor.Blue;
if (hcolor == purple
Console.BackgroundColor = ConsoleColor.DarkMagenta;
if (hcolor == black
Console.BackgroundColor = ConsoleColor.Black;
if (hcolor == white
Console.BackgroundColor = ConsoleColor.White;
if (color == cyan
Console.BackgroundColor = ConsoleColor.Cyan;
if (color == darkblue
Console.BackgroundColor = ConsoleColor.DarkBlue;
if (hcolor == darkred
Console.BackgroundColor = ConsoleColor.DarkRed;
if (hcolor == darkyellow
Console.BackgroundColor = ConsoleColor.DarkYellow;
if (hcolor == darkgreen
Console.BackgroundColor = ConsoleColor.DarkGreen;
}
public bool userbool = ;
public bool rojusbool = false ;
}
}





好​​的,所以最后我为这些帐户获得了2个公共bool。一开始我得到了登录的东西。正如你看到2个账户。每当我在rojus帐户中输入命令时,一切正常,但是当我在用户中键入命令时,我甚至看不到路径,也无法输入任何内容。

解决方案

所以使用调试器,看看发生了什么。

既然你没有显示执行任何这些的代码,我们可以帮助很多 - 我们没有想知道什么顺序完成了。但是假设这一切都是登录方法的工作原理,在它的顶部放置一个断点并逐步完成代码,在执行之前确定每条指令应该做什么。它做了你所期望的吗?如果是的话,继续下一步。如果没有,那么看看为什么不。



这是一项技能,需要用来正确学习。所以开始使用它!





BTW:删除标签和所有goto指令,改为使用循环结构。然后忘记goto这个词存在几年,直到你明白它应该被使用的时候 - 这不是一个合适的例子!


请参阅johannesnestler对问题的评论和我。你真的应该停止进行身份验证(你还没准备好)并专注于编程的基础知识。



关于使用密码进行身份验证,主要原则是:没有人能够知道他们,无论这个人有多少访问系统。你甚至可能认为这是不可能的。再想一想:问题通常使用加密哈希函数来解决。请查看我过去的答案:

我已经加密了密码但是当我登录时给出了我错了。如何解密 [ ^ ] ,

解密加密密码 [ ^ ],

存储密码值int sql server with secure方式 [ ^ ]。



我没有解决代码中的错误,抱歉。这不值得做。我最好的建议是:写一些其他代码,任何可以帮助你逐步学习编程的东西。



-SA

First I'll paste the code and then explain what I need.

        public void login()
        {
        sign:
            Console.Write("\nPlease enter your username(e.g. user - the default user username): ");
            var username = Console.ReadLine();
            Console.Write("\nPlease enter your passsword(e.g. 1111 - the default user password): ");
            var password = Console.ReadLine();
            if (username == "rojus")
                if (password == "dev")
                {
                    rojusbool = true;
                    InSys();
                }
            if (username == "user")
                if (password == "1111")
                {
                    userbool = true;
                    InSysuser();
                }

            if (username != "rojus")
                if(username != "user")
            {
                Console.Write("\nInvalid username");
                goto sign;
            }
            if (password != "dev")
                if (password != "1111")
            {
                Console.Write("\nInvalid password");
                goto sign;
            }

        }

        public void InSys()
        {
            if (rojusbool == true)
            {
            Console.Write("/TOS/Rojus@:");
            string help;
            help = Console.ReadLine();
            if (help == "help")
                Console.WriteLine("\nHelp - Displays a list of commands\nlogout - Logs out\nwhoami - Tells who are you\n sd - Shuts down\n colorsettings - Change your text color or your text highlight color.");
            if (help == "logout")
            {
                rojusbool = false;
                login();
            }
            if (help == "whoami")
                Console.WriteLine("\nYou are Rojus Lapinskas, a C# programmer");
            if (help == "sd")
                shutdown();
            if (help == "colorsettings")
                Colorpack();
            if (help != "help")
                if (help != "logout")
                    if (help != "whoami")
                        if (help != "sd")
                            if (help != "colorsettings")
                                Console.WriteLine("Command unrecognised");
            }
  

        }
        public void InSysuser()
        {
            if (userbool == true)
            {

                Console.Write("/TOS/User@:");
                string help;
                help = Console.ReadLine();
                if (help == "help")
                    Console.WriteLine("\nHelp - Displays a list of commands\nlogout - Logs out\nwhoami - Tells who are you\n sd - Shuts down\n colorsettings - Change your text color or your text highlight color.");
                if (help == "logout")
                {
                    userbool = false;
                    login();
                }
                if (help == "whoami")
                    Console.WriteLine("\nYou are a user, a user with basic rights");
                if (help == "sd")
                    shutdown();
                if (help == "colorsettings")
                    Colorpack();                  
                if (help != "help")
                    if (help != "logout")
                        if (help != "whoami")
                            if (help != "sd")
                                if (help != "colorsettings")
                                    Console.WriteLine("Command unrecognised");
            }
        }
  
        public void shutdown()
        {
            Console.Clear();
            Console.Write("\n");
            Console.Write("\nPress enter and shut down the computer or virtual machine\n");
            Console.ReadLine();
            while (1 == 1)
            {
                Console.Clear();
            }
        }
        public void Colorpack()
        {
            Console.Write("\nEnter the text color: ");
            var color = Console.ReadLine();
            if (color == "red")
                Console.ForegroundColor = ConsoleColor.Red;
            if (color == "orange")
                Console.ForegroundColor = ConsoleColor.Cyan;
            if (color == "yellow")
                Console.ForegroundColor = ConsoleColor.Yellow;
            if (color == "green")
                Console.ForegroundColor = ConsoleColor.Green;
            if (color == "blue")
                Console.ForegroundColor = ConsoleColor.Blue;
            if (color == "purple")
                Console.ForegroundColor = ConsoleColor.DarkMagenta;
            if (color == "black")
                Console.ForegroundColor = ConsoleColor.Black;
            if (color == "white")
                Console.ForegroundColor = ConsoleColor.White;
            if (color == "cyan")
                Console.ForegroundColor = ConsoleColor.Cyan;
            if (color == "darkblue")
                Console.ForegroundColor = ConsoleColor.DarkBlue;

            //Set up background color
            Console.Write("Enter the background color of the text: ");
            var hcolor = Console.ReadLine();

            if (hcolor == "red")
                Console.BackgroundColor = ConsoleColor.Red;
            if (hcolor == "orange")
                Console.BackgroundColor = ConsoleColor.Cyan;
            if (hcolor == "yellow")
                Console.BackgroundColor = ConsoleColor.Yellow;
            if (hcolor == "green")
                Console.BackgroundColor = ConsoleColor.Green;
            if (hcolor == "blue")
                Console.BackgroundColor = ConsoleColor.Blue;
            if (hcolor == "purple")
                Console.BackgroundColor = ConsoleColor.DarkMagenta;
            if (hcolor == "black")
                Console.BackgroundColor = ConsoleColor.Black;
            if (hcolor == "white")
                Console.BackgroundColor = ConsoleColor.White;
            if (color == "cyan")
                Console.BackgroundColor = ConsoleColor.Cyan;
            if (color == "darkblue")
                Console.BackgroundColor = ConsoleColor.DarkBlue;
            if (hcolor == "darkred")
                Console.BackgroundColor = ConsoleColor.DarkRed;
            if (hcolor == "darkyellow")
                Console.BackgroundColor = ConsoleColor.DarkYellow;
            if (hcolor == "darkgreen")
                Console.BackgroundColor = ConsoleColor.DarkGreen;
        }
        public bool userbool = false;
        public bool rojusbool = false;
    }
}



Ok, so at the end I got 2 public bools for the accounts. At the start I got the login stuff. As you see 2 accounts. Whenever I type a command in rojus account, everything works fine, however when I type a command in user, then I don't even see the path and I can't type anything.

解决方案

So use the debugger, and look at what is happening.
Since you don't show the code that executes any of that, we can;t help much - we have no idea what order things get done in. But assuming that it all works form the "login" method, put a breakpoint in the top of that and step your way through the code, working out what each instruction should do before you execute it. Did it do what you expected? If it did, move on to the next. If it didn't, then look at why not.

This is a skill, and it need to be used to be learned properly. So start using it!


BTW: Remove the label and all the "goto" instructions, and use loop constructs instead. Then forget the word "goto" exists for a few years until you understand when it should be used - and this is not a suitable example!


Please see the comments to the question by johannesnestler and myself. You really should stop doing authentication (you are not yet ready) and focus on the very basics of programming.

As to the using passwords for authentication, the main principle is: no one should be able to know them, no matter how much access to the system such person have. You may even think that it would not be possible. Think again: the problem is usually solved using cryptographic hash function. Please see my past answers:
i already encrypt my password but when i log in it gives me an error. how can decrypte it[^],
Decryption of Encrypted Password[^],
storing password value int sql server with secure way[^].

I did not address the bugs in your code, sorry. It's just not worth doing. My best advice would be: write some other code, anything which can help you to learn programming step by step.

—SA


这篇关于我需要C#Console中的帐户帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆