Skip to content
Related Articles
Open in App
Not now

Related Articles

C# Program to Illustrate User Authentication

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 18 Oct, 2022
Improve Article
Save Article

To illustrate User Authentication using C#, We will go through the Sign-Up and login functionality. Firstly the user needs to make an account (Sign UP) and then the user can go to the login activity. Below are the steps for User Authentication.

Steps For User Authentication:

Step 1: START

Step 2: Take user details like name, username, and password.

Step 3: Verify Password with constraint.

  • Password Length must be greater than or equal to 8 characters.
  • Password should be made up of alphanumeric characters and at least 1 special symbol.
  • Password Should not be more than 16 characters.

Step 4: If Step 4 is successful then show the message Account Created else repeat Step 2 and Step 3 until Step 3 does not return Successful.

Step 5: Take login credentials and verify if it correct or not.

  • if credentials are Correct: Show Logged in Successfully.
  • else show Entered wrong Username Or Password.

Step 6: STOP

Example 1:

C#




// C# implementation of User Authentication
// Include namespace system
using System;
using System.Text.RegularExpressions;
public class GFG
{
    public const String NAME = "GEEKSFORGEEKS";
    public const String USERNAME = "GEEKS";
    public const String PASSWORD = "Geeks@123";
    public static bool SignUp(String name, String userName, String password)
    {
        // Verify password length
        if (password.Length < 8 || password.Length > 16)
        {
            Console.WriteLine("Password must be in range of 8-16 characters.");
            return false;
        }
          Regex regex = new Regex("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]+");
  
      if (regex.Match(password).Value=="")
        {
            Console.WriteLine("Password must contains alphanumeric characters and atleast 1 special symbol.");
            return false;
        }
          
        return true;
    }
    public static bool Login(String userName, String password)
    {
        if (userName == USERNAME && password == PASSWORD)
        {
            return true;
        }
        Console.WriteLine("Enter Valid Username Or Password!");
        return false;
    }
    public static void Main(String[] args)
    {
        // Try SignUp With Invalid Password    (Length < 8)
        Console.WriteLine("User 1 : ");
        if (GFG.SignUp(NAME, USERNAME, "Geeks12"))
        {
            Console.WriteLine("Account Created Successfully.");
        }
        // Try SignUp With Invalid Password    (Excluding Special Symbol)
        Console.WriteLine("User 2 : ");
        if (GFG.SignUp(NAME, USERNAME, "Geeks123"))
        {
            Console.WriteLine("Account Created Successfully.");
        }
        // With Valid Password
        Console.WriteLine("User 3 : ");
        if (GFG.SignUp(NAME, USERNAME, PASSWORD))
        {
            Console.WriteLine("Account Created Successfully.");
        }
        // With Wrong Username Or Password
        Console.WriteLine("User 2 : ");
        if (GFG.Login(USERNAME, "Geeks123"))
        {
            Console.WriteLine("Log In Successful.");
        }
        // With Valid Username And Password
        Console.WriteLine("User 3 : ");
        if (GFG.Login(USERNAME, PASSWORD))
        {
            Console.WriteLine("Log In Successful.");
        }
    }
}

Output : 

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!