Enterprise Application: Login functionality in C#, Java, and Golang

Estimated read time 2 min read

Below are simple examples of a login functionality in C#, Java, and Golang. Note that these are very basic examples for educational purposes and may not include secure practices such as password hashing, authentication tokens, etc. Real-world login systems should prioritize security measures.

C# Login:

using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter username: ");
        string username = Console.ReadLine();

        Console.Write("Enter password: ");
        string password = Console.ReadLine();

        if (AuthenticateUser(username, password))
        {
            Console.WriteLine("Login successful in C#");
        }
        else
        {
            Console.WriteLine("Login failed. Invalid credentials.");
        }
    }

    static bool AuthenticateUser(string username, string password)
    {
        // Add your authentication logic here (e.g., check against a database).
        // This is a simplified example for educational purposes.
        return username == "example" && password == "password";
    }
}

Java Login:

import java.util.Scanner;

public class LoginExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter username: ");
        String username = scanner.nextLine();

        System.out.print("Enter password: ");
        String password = scanner.nextLine();

        if (authenticateUser(username, password)) {
            System.out.println("Login successful in Java");
        } else {
            System.out.println("Login failed. Invalid credentials.");
        }
    }

    static boolean authenticateUser(String username, String password) {
        // Add your authentication logic here (e.g., check against a database).
        // This is a simplified example for educational purposes.
        return username.equals("example") && password.equals("password");
    }
}

Golang (Go) Login:

package main

import (
    "fmt"
    "os"
    "bufio"
    "strings"
)

func main() {
    reader := bufio.NewReader(os.Stdin)

    fmt.Print("Enter username: ")
    username, _ := reader.ReadString('\n')
    username = strings.TrimSpace(username)

    fmt.Print("Enter password: ")
    password, _ := reader.ReadString('\n')
    password = strings.TrimSpace(password)

    if authenticateUser(username, password) {
        fmt.Println("Login successful in Golang")
    } else {
        fmt.Println("Login failed. Invalid credentials.")
    }
}

func authenticateUser(username, password string) bool {
    // Add your authentication logic here (e.g., check against a database).
    // This is a simplified example for educational purposes.
    return username == "example" && password == "password"
}

These examples demonstrate a basic console-based login system in each language. In a real-world scenario, you would integrate this authentication logic with a secure backend server and use proper security practices for handling user credentials.

Related Articles