Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Xml;
  7. using System.Xml.Serialization;
  8. using System.Security.Cryptography;
  9.  
  10. namespace ConsoleApplication1
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             List<User> users;
  17.             XmlSerializer serializer = new XmlSerializer(typeof(List<User>));
  18.  
  19.             string filename = "Users.xml";
  20.            
  21.             // Get the current user list, or initialize a new list.
  22.             if (File.Exists(filename))
  23.             {
  24.                 using (Stream stream = File.Open(filename, FileMode.Open))
  25.                 {
  26.                     users = (List<User>)serializer.Deserialize(stream);
  27.                 }
  28.             }
  29.             else
  30.             {
  31.                 users = new List<User>();
  32.             }
  33.            
  34.             // Create a new User
  35.             User newUser = CreateUser();
  36.             users.Add(newUser);
  37.  
  38.             // Save the users to the file again.
  39.             using (Stream stream = File.Open(filename, FileMode.OpenOrCreate))
  40.             {
  41.                 serializer.Serialize(stream, users);
  42.             }
  43.  
  44.             // Attempt Login
  45.             User loginUser = LoginUser();
  46.             if (users.Contains(loginUser))
  47.             {
  48.                 Console.WriteLine("Authenticated");
  49.             }
  50.             else
  51.             {
  52.                 Console.WriteLine("Invalid Login");
  53.             }
  54.         }
  55.  
  56.         private static User CreateUser()
  57.         {
  58.             Console.WriteLine("Create User");
  59.  
  60.             Console.Write("Username: ");
  61.             string username = Console.ReadLine();
  62.             Console.Write("Password: ");
  63.             string password = Console.ReadLine();
  64.  
  65.             return new User(username, SHA1(password));
  66.         }
  67.  
  68.         private static User LoginUser()
  69.         {
  70.             Console.WriteLine("Login");
  71.  
  72.             Console.Write("Username: ");
  73.             string username = Console.ReadLine();
  74.             Console.Write("Password: ");
  75.             string password = Console.ReadLine();
  76.  
  77.             return new User(username, SHA1(password));
  78.         }
  79.  
  80.         private static string SHA1(string input)
  81.         {
  82.             var provider = SHA1CryptoServiceProvider.Create();
  83.            
  84.             byte[] hash = provider.ComputeHash(
  85.                 Encoding.Default.GetBytes(input));
  86.            
  87.             return Convert.ToBase64String(hash);
  88.         }
  89.     }
  90.  
  91.     public class User : IEquatable<User>
  92.     {
  93.         public string Username
  94.         {
  95.             get;
  96.             set;
  97.         }
  98.  
  99.         public string Password
  100.         {
  101.             get;
  102.             set;
  103.         }
  104.  
  105.         public User()
  106.         {
  107.         }
  108.  
  109.         public User(string username, string password)
  110.         {
  111.             this.Username = username;
  112.             this.Password = password;
  113.         }
  114.  
  115.         #region IEquatable<User> Members
  116.  
  117.         public bool Equals(User other)
  118.         {
  119.             return (this.Username == other.Username)
  120.                 && (this.Password == other.Password);
  121.         }
  122.  
  123.         #endregion
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement