private static void WritetoXmlFile(List <Customer> customers, string passwordToEncrypt)
        {
            // define an XML file to write to
            string xmlFile = Combine(CurrentDirectory, "..", "protected-customers.xml");

            var xmlWriter = XmlWriter.Create(xmlFile, new XmlWriterSettings {
                Indent = true
            });

            xmlWriter.WriteStartDocument();
            xmlWriter.WriteStartElement("customers");

            foreach (Customer customer in customers)
            {
                xmlWriter.WriteStartElement("customer");
                xmlWriter.WriteElementString("name", customer.Name);

                xmlWriter.WriteElementString("creditcard", Protector.Encrypt(customer.CreditCard, passwordToEncrypt));

                // to protect the password we must salt and hash it, and store the random salt used
                var user = Protector.Register(customer.Name, customer.Password);
                xmlWriter.WriteElementString("password", user.SaltedHashedPassword);
                xmlWriter.WriteElementString("salt", user.Salt);
                xmlWriter.WriteEndElement(); // customer
            }
            xmlWriter.WriteEndElement();     // customers
            xmlWriter.WriteEndDocument();
            xmlWriter.Close();
        }
示例#2
0
        static void Main(string[] args)
        {
            WriteLine("Registering Alice with Pa$$w0rd.");
            var alice = Protector.Register("Alice", "Pa$$w0rd");

            WriteLine($"Name: {alice.Name}");
            WriteLine($"Salt: {alice.Salt}");
            WriteLine("Password (salted and hashed): {0}", alice.SaltedHashedPassword);
            WriteLine();
        }
        public void RegisterTest()
        {
            var alice = Protector.Register("alice", "mypass");

            Console.WriteLine($"Name: {alice.Name}");
            Console.WriteLine($"Salt: {alice.Salt}");
            Console.WriteLine($"SaltedHash: {alice.SaltedHashedPassword}");

            Assert.IsTrue(Protector.CheckPassword("alice", "mypass"));
            Assert.IsFalse(Protector.CheckPassword("alice", "notpass"));
        }
        public static void Run()
        {
            Protector.Register("Alice", "Pa$$w0rd", new[] { "Admins" });
            Protector.Register("Bob", "Pa$$w0rd",
                               new[] { "Sales", "TeamLeads" });
            Protector.Register("Eve", "Pa$$w0rd");

            Write($"Enter your user name: ");
            string username = ReadLine();

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

            var p = Thread.CurrentPrincipal;

            WriteLine($"IsAuthenticated: {p.Identity.IsAuthenticated}");
            WriteLine($"AuthenticationType: {p.Identity.AuthenticationType}");
            WriteLine($"Name: {p.Identity.Name}");
            WriteLine($"IsInRole(\"Admins\"): {p.IsInRole("Admins")}");
            WriteLine($"IsInRole(\"Sales\"): {p.IsInRole("Sales")}");

            if (p is ClaimsPrincipal)
            {
                WriteLine($"{p.Identity.Name} has the following claims:");
                foreach (Claim claim in (p as ClaimsPrincipal).Claims)

                {
                    WriteLine($"{claim.Type}: {claim.Value}");
                }
            }

            try
            {
                SecureFeature();
            }
            catch (System.Exception ex)
            {
                WriteLine($"{ex.GetType()}: {ex.Message}");
            }
        }
示例#5
0
        static void Main(string[] args)
        {
            WriteLine("Registering Alice with Pa$$w0rd.");
            var alice = Protector.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     = Protector.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 = Protector.CheckPassword(
                    loginUsername, loginPassword);

                if (correctPassword)
                {
                    WriteLine($"Correct! {loginUsername} has been logged in.");
                }
                else
                {
                    WriteLine("Invalid username or password. Try again.");
                }
            }
        }
        static void Main(string[] args)
        {
            WriteLine("Registering [email protected] with Pa$$w0rd");

            var alice = Protector.Register("*****@*****.**", "Pa$$w0rd");

            WriteLine($"Name: {alice.Name}");
            WriteLine($"Salt: {alice.Salt}");
            WriteLine($"SaltedAndHashed password: {alice.SaltedHashedPassword}\n");

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

            WriteLine($"Enter password for {username}: ");
            string password = ReadLine();

            var newUser = Protector.Register(username, password);

            WriteLine($"New user: {newUser.Name}");
            WriteLine($"Salt: {newUser.Salt}");
            WriteLine($"SaltedAndHashed password: {newUser.SaltedHashedPassword}\n");


            bool correctPassword = false;

            while (!correctPassword)
            {
                WriteLine($"Enter a username to log in with: ");
                string loginUsername = ReadLine();
                WriteLine($"Enter the password for {loginUsername}: ");
                string loginPassword = ReadLine();
                correctPassword = Protector.CheckPassword(loginUsername, loginPassword);
                if (correctPassword)
                {
                    WriteLine($"{loginUsername} has successfully logged in.");
                }
                else
                {
                    WriteLine($"{loginUsername} cannot log in with details provided. Try again. \n");
                }
            }

            WriteLine("End of HashingApp :-)");
        }
示例#7
0
        static void Main(string[] args)
        {
            WriteLine("Registering Teisel UwU with Pa$$w0rd");
            var teisel = Protector.Register("Teisel", "Pa$$w0rd");

            WriteLine($"Name {teisel.Name}");
            WriteLine($"Salt : {teisel.Salt}");
            WriteLine($"Password (Salted and hashed) :  {teisel.SaltedHashedPassword}");

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

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

            WriteLine($"Name {user.Name}");
            WriteLine($"Salt : {user.Salt}");
            WriteLine($"Password (Salted and hashed) :  {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 = Protector.CheckPassword(loginUsername, loginPassword);

                if (correctPassword)
                {
                    WriteLine($"Welcome back! {loginUsername} has been logged in");
                }
                else
                {
                    WriteLine("Invalid username or password");
                }
            }
        }
示例#8
0
        static void Main(string[] args)
        {
            Console.WriteLine("A user named Alice has been registered with Pas$$w0rd as her password.");
            var alice = Protector.Register("Alice", "Pa$$w0rd");

            Console.WriteLine($"Name: {alice.Name}");
            Console.WriteLine($"Salt: {alice.Salt}");
            Console.WriteLine($"Salted hashed password: {alice.SaltedHashedPassword}");
            Console.WriteLine();

            Console.WriteLine("Enter a username to register: ");
            string username = Console.ReadLine();

            Console.WriteLine("Enter a password to register: ");
            string password = Console.ReadLine();
            var    user     = Protector.Register(username, password);

            Console.WriteLine($"Name: {user.Name}");
            Console.WriteLine($"Salt: {user.Salt}");
            Console.WriteLine($"Salted hashed password: {user.SaltedHashedPassword}");
            Console.WriteLine();
            Console.WriteLine("---------------------------------------------------");
            Console.WriteLine();

            bool correctPassword = false;

            while (!correctPassword)
            {
                Console.WriteLine("Enter a username to login: "******"Enter a password to login: "******"Correct! {loginUsername} has been logged in.");
                }
                else
                {
                    Console.WriteLine("Invalid username or password. Try again.");
                }
            }
        }
示例#9
0
        static void Main(string[] args)
        {
            Console.WriteLine("================== Registration ==================");
            Write("Name: ");
            var name = ReadLine();

            Write("Password: "******"Name: " + user.Name);
            WriteLine("Salt: " + user.Salt);
            WriteLine("SaltedHashedPassword: "******"================== Password Check ==================");
            bool isPasswordCorrect = false;

            while (!isPasswordCorrect)
            {
                Write("Enter Name: ");
                var nameInput = ReadLine();
                Write("Enter password: "******"Passsword Correct! =)");
                }
                else
                {
                    WriteLine("Wrong password. Please try again:");
                }
            }
        }