Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- public class Challenge
- {
- string localdb;
- string username;
- int privilege = 0;
- static public void Main()
- {
- Console.WriteLine("Challenge (TM) Console");
- Console.WriteLine("======================");
- Challenge chl = new Challenge();
- if (chl.privilege == 0)
- {
- chl.privilege = chl.do_login();
- if (chl.privilege == 0)
- {
- Console.WriteLine("Tidak dapat mengenali Anda. Apakah Anda terdaftar?");
- return;
- }
- }
- if (chl.privilege != 0)
- {
- Console.Write("Selamat datang, " + chl.username);
- switch (chl.privilege)
- {
- case 1: Console.WriteLine(" (User)"); break;
- case 2: Console.WriteLine(" (Admin)");
- break;
- }
- }
- }
- public Challenge()
- {
- localdb = "credentials.db";
- }
- int do_login()
- {
- string user;
- string password;
- string pass;
- int priv;
- bool found = false;
- if (privilege != 0)
- {
- return privilege;
- }
- privilege = 0;
- Console.WriteLine("Please Login\n\n");
- Console.Write("Username: ");
- username = Console.ReadLine();
- Console.Write("Password: ");
- password = Console.ReadLine();
- //#1 Check if there is local cache
- if (!found)
- {
- string line;
- try
- {
- using (TextReader reader = File.OpenText(localdb))
- {
- while ((line = reader.ReadLine()) != null)
- {
- string[] words = line.Split('|');
- if (words.Length >= 3)
- {
- // Username
- user = words[0];
- // password
- pass = words[1];
- // privilege
- priv = int.Parse(words[2]);
- if (username == user && password == pass)
- {
- found = true;
- privilege = priv;
- break;
- }
- }
- }
- }
- } catch
- {
- found = false;
- privilege = 0;
- }
- }
- return privilege;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement