隐藏/替换输入密码时的字符 (C#)

13

好的,我是C#的新手,但已经学到了一些。但我有一个问题,如何将在控制台输入的字符替换为“*”或完全隐藏它们?

        var pw = "eric123";
        Console.WriteLine("Password: ");
        var value = Console.ReadLine();
        if (value == pw)
        {
            Console.WriteLine("Permitted, Play online? (Y/N)?");
            var getGameOnlineStatus = Console.ReadLine();

            //Rest Of the Code is just for me :)

希望能得到任何帮助!


1
http://www.c-sharpcorner.com/forums/thread/32102/password-in-C-Sharp-console-application.aspx - Joel Gregory
3个回答

23

我在这里找到了

C#控制台应用程序中的密码屏蔽

class PasswordExample
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Pls key in your Login ID");
            var loginid = Console.ReadLine();
            Console.WriteLine("Pls key in your Password");
            var password = ReadPassword();
            Console.Write("Your Password is:" + password);
            Console.ReadLine();
        }


     public static string ReadPassword()
        {
            string password = "";
            ConsoleKeyInfo info = Console.ReadKey(true);
            while (info.Key != ConsoleKey.Enter)
            {
                if (info.Key != ConsoleKey.Backspace)
                {
                    Console.Write("*");
                    password += info.KeyChar;
                }
                else if (info.Key == ConsoleKey.Backspace)
                {
                    if (!string.IsNullOrEmpty(password))
                    {
                        // remove one character from the list of password characters
                        password = password.Substring(0, password.Length - 1);
                        // get the location of the cursor
                        int pos = Console.CursorLeft;
                        // move the cursor to the left by one character
                        Console.SetCursorPosition(pos - 1, Console.CursorTop);
                        // replace it with space
                        Console.Write(" ");
                        // move the cursor to the left by one character again
                        Console.SetCursorPosition(pos - 1, Console.CursorTop);
                    }
                }
                info = Console.ReadKey(true);
            }
            // add a new line because user pressed enter at the end of their password
            Console.WriteLine();
            return password;
        }
    }

0
    public static string HideCharacter()
    {
        ConsoleKeyInfo key;
        string code = "";
        do
        {
            key = Console.ReadKey(true);

            if (Char.IsNumber(key.KeyChar))
            {
                    Console.Write("*");
            }
            code += key.KeyChar;
        } while (key.Key != ConsoleKey.Enter);

        return code;

    }

password = HideCharacter();


0
using System;

class Login{

  static void Main() {

    string passwd = HideCharacter();
    Console.WriteLine(passwd);

  }

   public static string HideCharacter()

    {
        ConsoleKeyInfo key;
        string code = "";
        do
        {
            key = Console.ReadKey(true);
            if (Char.IsNumber(key.KeyChar)||Char.IsLetter(key.KeyChar))
            {
                    Console.Write("*");
                    code += key.KeyChar;
            }
        } while (key.Key != ConsoleKey.Enter);
        return code;
    }
}

请记住,Stack Overflow 不仅旨在解决当前的问题,还要帮助未来的读者找到类似问题的解决方案,这需要理解底层代码。对于我们社区中的初学者和不熟悉语法的成员来说,这尤其重要。鉴于此,您能否编辑您的答案,包括您正在做什么的解释以及为什么您认为这是最好的方法? - Jeremy Caney

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接