Sunday, June 28, 2020

Login System in C#

I wrote this program to show how to create a login system using C# and text files.

I am currently accepting programming work inventory system, enrollment system, accounting system, payroll system, information system, website design and development using WordPress, IT projects, school and application development, programming projects, thesis, and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details. If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.My mobile number here in the Philippines is 09173084360. My telephone number at home here in Bacolod City, Negros Occidental Philippines is +63 (034) 4335675.

Here in Bacolod City, Negros Occidental I also accepting computer repair, web development using WordPress, Computer Networking, and Arduino Project development at a very affordable price. My personal website is http://www.jakerpomperada.comMy programming website is http://www.jakerpomperada.blogspot.comI am also a book author you can purchase my books on computer programming and information technology in the following links below.https://www.unlimitedbooksph.com/



Sample Program Output


Program Listing

login.txt

jake jake
admin admin
tom abc
iya iya
jacob jacob
allie allie

UserManager.cs

using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

namespace LoginSystem
{
  class UserManager
  {
    public void Load(string filename)
    {
      Debug.Assert(!string.IsNullOrEmpty(filename));
      Debug.Assert(File.Exists(filename));
      string[] lines = File.ReadAllLines(filename);
      foreach (string line in lines)
      {
        string[] tokens = line.Split('\t');
        if (tokens.Length == 2)
          credentials.Add(tokens[0], tokens[1]);
        else
          Debug.WriteLine("Read invalid line: {}", line);
      }
    }
    public bool ValidateUser(string user, string password)
    {
      if (!credentials.ContainsKey(user))
        return false;
      else
        return credentials[user] == password;
    }
    IDictionary<string, string> credentials = new Dictionary<string, string>();
  }
}


ConsoleUtils.cs

using System;
using System.Text;

namespace LoginSystem
{
  static class ConsoleUtils
  {
    public static string ReadPassword(char display)
    {
      StringBuilder sb = new StringBuilder();

      while (true)
      {
        ConsoleKeyInfo key = Console.ReadKey(true);
        if (key.Key == ConsoleKey.Enter)
          break;

        sb.Append(key.KeyChar);
        Console.Write(display);
      }

      return sb.ToString();
    }
  }
}

Main.cs

using System;

namespace LoginSystem
{
  class Program
  {
    static void Main(string[] args)
    {
      UserManager mgr = new UserManager();
      mgr.Load("login.txt");

      Console.Write("\n\n");
      Console.Write("\t\tLogin System in C#\n\n");
      Console.Write("\tCreated By Jake Rodriguez Pomperada\n");
      Console.Write("\n");
      Console.Write("\tEnter Username  : ");
      string user = Console.ReadLine();
      Console.Write("\tEnter Password  : ");
      string pw = ConsoleUtils.ReadPassword('$');
      Console.WriteLine("\n");
      
      if (mgr.ValidateUser(user, pw))
        Console.WriteLine("\tLogin successful to the system.");
      else
        Console.WriteLine("\tEither username or password is wrong. Try Again.");
      Console.ReadKey();
    }
  }
}




No comments:

Post a Comment