C-Authentication program will helps to add an authentic step before getting into your program. This can be used as a security check in any C program.
Customization In Code
In the given code for Authentication in C language, you can specify following properties/values as per your choice.
- Username
- Password
- Number of maximum attempts
Source Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
#include< conio.h > #include< stdio.h > #include< dos.h > #include< string.h > int authenticate(char userName[], char password[]); void main(){ char password[20] = "password"; // set password char userName[20] = "userName"; // set userName if(authenticate(userName,password)){ printf("\nAuthentication Successfull!"); }else{ printf("\nYou have attempted all tries, program will exit soon!!!"); sleep(3); exit(1); } getch(); } int authenticate(char userName[], char password[]){ char ch; char currentUserName[20]; char currentPassword[20]; int i,attempts=1,userNameCorrect=1; int maxTry = 3; while(attempts < = maxTry){ ch='\0'; if(userNameCorrect){ clrscr(); printf("User Name : "); gets(currentUserName); } if(strcmp(currentUserName,userName)==0){ userNameCorrect = 0; clrscr(); printf("User Name : %s\n",currentUserName); printf("Password : "); i=0; while(ch!=13){ ch = getch(); if(ch!=13 && ch!=8){ putch('*'); currentPassword[i]=ch; i++; } } currentPassword[i] = '\0'; if(strcmp(currentPassword,password)==0){ // Authentication is successfull; return 1; }else{ printf("\nPassword incorrect!"); getch(); attempts++; } }else{ attempts++; printf("User name incorrect!"); getch(); } } return 0; } |