Ejemplo n.º 1
0
        /// <summary>
        /// Simulated Authentication
        /// </summary>
        /// <returns></returns>
        static bool DoLogin()
        {
            Write($"Enter your user name: ");
            string username = ReadLine();

            Write($"Enter your password: "******"Log in failed.");
                return(true);
            }

            return(false);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            WriteLine("Registering Alice with Pa$$w0rd.");
            var alice = AuthProtector.Register("Alice", "Pa$$w0rd");

            WriteLine($"Name: {alice.Name}");
            WriteLine($"Salt: {alice.Salt}");
            WriteLine("Password (salted and hashed): {0}",
                      arg0: alice.SaltedHashedPassword);
            WriteLine();

            Write("Enter a new user to register: ");
            string username = ReadLine();

            Write($"Enter a password for {username}: ");
            string password = ReadLine();
            var    user     = AuthProtector.Register(username, password);

            WriteLine($"Name: {user.Name}");
            WriteLine($"Salt: {user.Salt}");
            WriteLine("Password (salted and hashed): {0}",
                      arg0: user.SaltedHashedPassword);
            WriteLine();

            bool correctPassword = false;

            while (!correctPassword)
            {
                Write("Enter a username to log in: ");
                string loginUsername = ReadLine();
                Write("Enter a password to log in: ");
                string loginPassword = ReadLine();
                correctPassword = AuthProtector.CheckPassword(
                    loginUsername, loginPassword);
                if (correctPassword)
                {
                    WriteLine($"Correct! {loginUsername} has been logged in.");
                }
                else
                {
                    WriteLine("Invalid username or password. Try again.");
                }
            }
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            AuthProtector.Register("Alice", "Pa$$w0rd", new[] { "Admins" });
            AuthProtector.Register("Bob", "Pa$$w0rd", new[] { "Sales", "TeamLeads" });
            AuthProtector.Register("Eve", "Pa$$w0rd");

            if (!DoLogin())
            {
                return;
            }

            ShowCurrentPrincipal();

            try
            {
                RunSecuredAdminFeature();
            }
            catch (System.Exception ex)
            {
                WriteLine($"{ex.GetType()}: {ex.Message}");
            }
        }