I write simple program, that starts another Client.exe from user:
Console.Write("Enter your domain: ");
string domain = Console.ReadLine();
Console.Write("Enter you user name: ");
string uname = Console.ReadLine();
Console.Write("Enter your password: ");
SecureString password = new SecureString();
ConsoleKeyInfo key;
do
{
key = Console.ReadKey(true);
// Ignore any key out of range.
if (((int)key.Key) >= 33 && ((int)key.Key <= 90) && key.Key != ConsoleKey.Enter)
{
// Append the character to the password.
password.AppendChar(key.KeyChar);
Console.Write("*");
}
// Exit if Enter key is pressed.
} while (key.Key != ConsoleKey.Enter);
Console.WriteLine();
try
{
Console.WriteLine("\nTrying to launch ProcessClient using your login information...");
Process.Start("ProcessClient.exe", uname, password, domain);
}
catch (Win32Exception ex)
{
Console.WriteLine(ex.Message);
}
And it works! But how Client.exe knows what user execute this program?
You can know this using the Environment class:
Environment.UserName
Gets the user name of the person who is currently logged on to the Windows operating system.
Environment.UserDomainName
Gets the network domain name associated with the current user.
Related
I am currently making a whitelist system program and whenever I add another line to the pastebin, the entire thing breaks on me leaving me to only be able to do 1 line.
string key = Console.ReadLine();
string whitelist = new System.Net.WebClient() { Proxy = null }.DownloadString("https://pastebin.com/raw/shzKS0RB");
string blacklist = new System.Net.WebClient() { Proxy = null }.DownloadString("https://pastebin.com/raw/gQmWxkjA");
if (key.Contains(whitelist))
{
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(" [WHITELISTED] YOUR KEY HAS BEEN WHITELISTED SUCCESSFULLY!");
Thread.Sleep(2000);
Console.Clear();
goto Panel;
}
if (key.Contains(blacklist))
{
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine(" [BLACKLISTED] YOUR KEY IS UNFORTUNATELY BLACKLISTED FROM OUR SYSTEM!");
Thread.Sleep(2000);
Console.Clear();
Environment.Exit(-1);
}
else
{
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine(" [INVALID KEY] THE KEY YOU HAVE INPUTTED IS INVALID!");
Thread.Sleep(2000);
Console.Clear();
goto key;
}
But inspecting the data in a web browser shows me the items in separate lines
<pre>R93UBR-20QM48-QME677-P4KGM1-7L0GKY
testkey2
testkey3
testkey4</pre>
So basically I'm trying to write a console application where it asks the user for a password and will continue to ask for it only three times and then stop with the use of a 'while' loop, but it keeps on looping and asking me for the password even if the correct password was used and after I entered it more that 3 times.
const string pass = "Password";
string attempt;
do
{
Console.Write("Please enter password: ");
attempt = Console.ReadLine();
if (attempt == pass)
{
Console.WriteLine("Access granted.");
}
else
{
Console.WriteLine("Access denied.");
}
} while (true);
const string pass = "Password";
string attempt;
int n = 0;
do
{
Console.Write("Please enter password: ");
attempt = Console.ReadLine();
if (attempt == pass)
{
Console.WriteLine("Access granted.");
break;
}
else
{
Console.WriteLine("Access denied.");
n++;
}
} while (n<=3);
So you want to ask user password three time until correct one entered and stop asking if user enter correct on. You can do it by for loop
const string pass = "Password";
string attempt;
for(int i=0;i<3;i++)
{
Console.Write("Please enter password: ");
attempt = Console.ReadLine();
if (attempt == pass)
{
Console.WriteLine("Access granted.");
i = 4;
}
else
{
Console.WriteLine("Access denied.");
}
};
You can do the following:
const string pass = "Password";
string attempt;
int attempt = 0;
do
{
Console.Write("Please enter password: ");
attempt = Console.ReadLine();
if (attempt == pass)
{
Console.WriteLine("Access granted.");
}
else
{
Console.WriteLine("Access denied.");
attempt++;
}
} while (attempt <= 3 && attempt != pass);
string pass = "";
while (pass != "password")
{
Console.WriteLine("enter your password here");
pass = Convert.ToString(Console.ReadLine());
if (pass == "password")
{
Console.WriteLine("your password is correct");
}
}
Console.ReadKey();
Hi I would like to prevent users from entering nothing in the input field.
I've tried using an if else but the console keeps crashing when there's no input. (for both user input and ldap address input ==> I want it to show "No input detected." and allow the user to re-enter the username)
And if I used (results == " "), I would get a error:
"Operator '==' cannot be applied to operands of type
'System.DirectoryServices.SearchResult' and 'string'"
Is there any way for me to resolve this? The codes are as shown below.
Affected codes from line 16 onwards (for the top block of codes)
if (results != null)
{
//Check is account activated
bool isAccountActived = IsActive(results.GetDirectoryEntry());
if (isAccountActived)
Console.WriteLine(targetUserName + "'s account is active.");
else
Console.WriteLine(targetUserName + "'s account is inactive.");
//Check is account expired or locked
bool isAccountLocked = IsAccountLockOrExpired(results.GetDirectoryEntry());
if (isAccountLocked)
Console.WriteLine(targetUserName + "'s account is locked or has expired.");
else
Console.WriteLine(targetUserName + "'s account is not locked or expired.");
Console.WriteLine("\nEnter bye to exit.");
Console.WriteLine("Press any key to continue.\n\n");
}
else if (results == " ")
{
//no user entered
Console.WriteLine("No input detected!");
Console.WriteLine("\nEnter bye to exit.");
Console.WriteLine("Press any key to continue.\n");
}
else
{
//user does not exist
Console.WriteLine("User not found!");
Console.WriteLine("\nEnter bye to exit.");
Console.WriteLine("Press any key to continue.\n");
}
If it helps, I've attached the whole code below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Data.SqlClient;
namespace ConsoleApplication2
{
class Program
{
const String serviceAccountUserName = "mobileuser1";
const String serviceAccountPassword = "password123$";
const int UF_LOCKOUT = 0x0010;
const int UF_PASSWORD_EXPIRED = 0x800000;
static void Main(string[] args)
{
string line;
Console.WriteLine("Welcome to account validator V1.0.\n"+"Please enter the ldap address to proceed.");
Console.Write("\nEnter address: ");
String ldapAddress = Console.ReadLine();
try
{
if (ldapAddress != null)
{
Console.WriteLine("\nQuerying for users in " + ldapAddress);
//start of do-while
do
{
Console.WriteLine("\nPlease enter the user's account name to proceed.");
Console.Write("\nUsername: ");
String targetUserName = Console.ReadLine();
bool isValid = false;
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, ldapAddress))
{
// validate the credentials
isValid = pc.ValidateCredentials(serviceAccountUserName, serviceAccountPassword);
// search AD data
DirectoryEntry entry = new DirectoryEntry("LDAP://" + ldapAddress, serviceAccountUserName, serviceAccountPassword);
//create instance fo the directory searcher
DirectorySearcher desearch = new DirectorySearcher(entry);
//set the search filter
desearch.Filter = "(&(sAMAccountName=" + targetUserName + ")(objectcategory=user))";
//find the first instance
SearchResult results = desearch.FindOne();
if (results != null)
{
//Check is account activated
bool isAccountActived = IsActive(results.GetDirectoryEntry());
if (isAccountActived) Console.WriteLine(targetUserName + "'s account is active.");
else Console.WriteLine(targetUserName + "'s account is inactive.");
//Check is account expired or locked
bool isAccountLocked = IsAccountLockOrExpired(results.GetDirectoryEntry());
if (isAccountLocked) Console.WriteLine(targetUserName + "'s account is locked or has expired.");
else Console.WriteLine(targetUserName + "'s account is not locked or expired.");
Console.WriteLine("\nEnter bye to exit.");
Console.WriteLine("Press any key to continue.\n\n");
}
else if (results == " ")
{
//no user entered
Console.WriteLine("No input detected!");
Console.WriteLine("\nEnter bye to exit.");
Console.WriteLine("Press any key to continue.\n");
}
else
{
//user does not exist
Console.WriteLine("User not found!");
Console.WriteLine("\nEnter bye to exit.");
Console.WriteLine("Press any key to continue.\n");
}
}//end of using
}//end of do
//leave console when 'bye' is entered
while ((line = Console.ReadLine()) != "bye");
}//end of if for ldap statement
else if (ldapAddress == " ")
{
Console.WriteLine("No input detected.");
Console.ReadLine();
Console.WriteLine("\nEnter bye to exit.");
Console.ReadLine();
Console.WriteLine("Press any key to continue.\n");
Console.ReadLine();
}
else
{
Console.WriteLine("Address not found!");
Console.ReadLine();
Console.WriteLine("\nEnter bye to exit.");
Console.ReadLine();
Console.WriteLine("Press any key to continue.\n");
Console.ReadLine();
}
}//end of try
catch (Exception e)
{
Console.WriteLine("Exception caught:\n\n" + e.ToString());
}
} //end of main void
static private bool IsActive(DirectoryEntry de)
{
if (de.NativeGuid == null) return false;
int flags = (int)de.Properties["userAccountControl"].Value;
return !Convert.ToBoolean(flags & 0x0002);
}
static private bool IsAccountLockOrExpired(DirectoryEntry de)
{
string attribName = "msDS-User-Account-Control-Computed";
de.RefreshCache(new string[] { attribName });
int userFlags = (int)de.Properties[attribName].Value;
return userFlags == UF_LOCKOUT || userFlags == UF_PASSWORD_EXPIRED;
}
}
}
You should put the ReadLine in a loop.
string UserName = "";
do {
Console.Write("Username: ");
UserName = Console.ReadLine();
if (!string.IsNullOrEmpty(UserName)) {
Console.WriteLine("OK");
} else {
Console.WriteLine("Empty input, please try again");
}
} while (string.IsNullOrEmpty(UserName));
You basically repeat the prompt over and over until the string entered by the user is no longer null or empty.
Best method would probably be to create a new function to get a non empty input:
private static string GetInput(string Prompt)
{
string Result = "";
do {
Console.Write(Prompt + ": ");
Result = Console.ReadLine();
if (string.IsNullOrEmpty(Result)) {
Console.WriteLine("Empty input, please try again");
}
} while (string.IsNullOrEmpty(Result));
return Result;
}
You can then just use the function to get your inputs like:
static void Main(string[] args)
{
GetInput("Username");
GetInput("Password");
}
Result:
Try using the code :
(!string.IsNullOrEmpty(input));
This is user first name and last name string
//Make container for the user first name and last name
string myFirstName = "";
string myLastName = "";
//Do while loop
do
{
//Welcomes user to the app and asks for first name then asks for last name
Console.WriteLine("Welcome");
Console.WriteLine("Enter first name: ");
//Takes users first name and last name and saves it in myFirstName and myLastName
myFirstName = Console.ReadLine();
Console.Write("Enter Last name: ");
myLastName = Console.ReadLine();
Console.WriteLine();
//If the first AND (&&) last name is not empty because the user entered first name and last name then display the hello message
if (!string.IsNullOrEmpty(myFirstName) && !string.IsNullOrEmpty(myLastName))
{
Console.WriteLine("Hello " + myFirstName + " " + myLastName + " hope you enjoy your day");
}
else //Else the first name or last name is left empty then display the error message
{
Console.WriteLine("Please enter your first name and last name");
Console.WriteLine();
}
//While if either the first name OR (||) last name is empty then keep asking the user for input
} while (string.IsNullOrEmpty(myFirstName) ||
string.IsNullOrEmpty(myLastName));
Console.ReadLine();
Output:
so I'm making a mock program just to get somewhat more used to c# and heres what i've got:
ConsoleKeyInfo Input;
StringBuilder sb = new StringBuilder();
const string Password = "class";
Console.Write("Input Your Password: ");
do
{
Input = Console.ReadKey(true);
sb.Append(Input.KeyChar);
} while (Input.Key != ConsoleKey.Enter);
Console.WriteLine();
if (sb.ToString() == Password)
{
Console.WriteLine("Correct Password!");
}
Console.WriteLine("You Entered: " + sb.ToString());
Console.WriteLine("The Pass is: " + Password);
Console.ReadLine();
But I have an issue with my if-statement when I come to compare sb.ToString() and Password. Although if you put the same thing in as Password the if-statement still doesn't become true.
Why is this?
Because you are also adding the Enter key to StringBuilder at the end, you can just check it before adding:
do
{
Input = Console.ReadKey(true);
if(Input.Key != ConsoleKey.Enter)
sb.Append(Input.KeyChar);
} while (Input.Key != ConsoleKey.Enter);
Or instead of checking it twice you can also refactor your loop like this:
while ((Input = Console.ReadKey(true)).Key != ConsoleKey.Enter)
sb.Append(Input.KeyChar);
How can I tell the program to give access only if I enterd the correct password?
Thank you.
namespace Password
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter password:");
Console.ReadLine();
string Password = "Test";
bool PassWordMatch;
PassWordMatch = Password == "Test";
if (PassWordMatch)
{
Console.WriteLine(" Password Match. Access Granted");
}
else
{
Console.WriteLine("Password doesn't match! Access denied.");
}
}
}
}
You could use the Console.ReadLine method which will return the value entered by the user and you could store it in the corresponding variable:
namespace Password
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter password:");
string password = Console.ReadLine();
bool passWordMatch;
passWordMatch = password == "Test";
if (passWordMatch)
{
Console.WriteLine(" Password Match. Access Granted");
}
else
{
Console.WriteLine("Password doesn't match! Access denied.");
}
}
}
}
You almost there.
Console.ReadLine() method reads the standart input strem and returns it as string. You just need to assing what this method return a new string and compare it your test password.
Console.WriteLine("Please enter password:");
string input = Console.ReadLine();
bool PassWordMatch = input == "Test";
if(PassWordMatch)
Console.WriteLine(" Password Match. Access Granted");
else
Console.WriteLine("Password doesn't match! Access denied.");
And of course, this is not a good way for security in your application.
You have not assigned the read string to a variable, so it is further unavailable for comparison.
Console.ReadLine() function can be used to read the next line of characters from the input stream, or returns null if no more lines are available.
You could do it as below:
namespace Password
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter password:");
string password = Console.ReadLine(); //Assign user-entered password
bool passWordMatch;
passWordMatch = password == "Test";
if (passWordMatch)
{
Console.WriteLine(" Password Match. Access Granted");
}
else
{
Console.WriteLine("Password doesn't match! Access denied.");
}
}
}
}