Advertisement
Guest User

Untitled

a guest
May 2nd, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.88 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. public class Challenge
  5. {
  6.     string localdb;
  7.     string username;
  8.     int    privilege = 0;
  9.    
  10.     static public void Main()
  11.     {
  12.         Console.WriteLine("Challenge (TM) Console");
  13.         Console.WriteLine("======================");
  14.        
  15.         Challenge chl = new Challenge();
  16.         if (chl.privilege == 0)
  17.         {
  18.             chl.privilege = chl.do_login();
  19.             if (chl.privilege == 0)
  20.             {
  21.                 Console.WriteLine("Tidak dapat mengenali Anda. Apakah Anda terdaftar?");
  22.                 return;
  23.             }
  24.         }
  25.        
  26.         if (chl.privilege != 0)
  27.         {
  28.             Console.Write("Selamat datang, " + chl.username);
  29.             switch (chl.privilege)
  30.             {
  31.                 case 1: Console.WriteLine(" (User)"); break;
  32.                 case 2: Console.WriteLine(" (Admin)");
  33.                 break;
  34.             }
  35.         }
  36.     }
  37.    
  38.     public Challenge()
  39.     {
  40.         localdb = "credentials.db";
  41.     }
  42.    
  43.     int do_login()
  44.     {
  45.         string user;
  46.         string password;
  47.         string pass;
  48.         int    priv;
  49.         bool   found = false;
  50.        
  51.         if (privilege != 0)
  52.         {
  53.             return privilege;
  54.         }
  55.        
  56.         privilege = 0;
  57.        
  58.         Console.WriteLine("Please Login\n\n");
  59.         Console.Write("Username: ");
  60.         username = Console.ReadLine();
  61.        
  62.         Console.Write("Password: ");
  63.         password = Console.ReadLine();
  64.        
  65.         //#1 Check if there is local cache
  66.         if (!found)
  67.         {
  68.             string line;
  69.             try
  70.             {
  71.                 using (TextReader reader = File.OpenText(localdb))
  72.                 {
  73.                     while ((line = reader.ReadLine()) != null)
  74.                     {
  75.                         string[] words = line.Split('|');
  76.                        
  77.                         if (words.Length >= 3)
  78.                         {
  79.                             // Username
  80.                             user = words[0];
  81.                            
  82.                             // password
  83.                             pass = words[1];
  84.                            
  85.                             // privilege
  86.                             priv = int.Parse(words[2]);
  87.                            
  88.                             if (username == user && password == pass)
  89.                             {
  90.                                 found = true;
  91.                                 privilege = priv;
  92.                                 break;
  93.                             }
  94.                         }
  95.                     }
  96.                 }
  97.             } catch
  98.             {
  99.                 found = false;
  100.                 privilege = 0;
  101.             }
  102.         }
  103.        
  104.         return privilege;      
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement