After thoroughly updating your post, I believe you could be after one of two things here:
- How to execute different commands based on user input?
- How to change a user's password on a server.
I will give a basic overview for each of these in hopes that you may become better educated, and hopefully will update your post to reflect your truly desired result.
The Amazing Switch
To execute multiple commands within your console application, look into the switch structure available in C#. This will allow you to execute different bits of code based on certain criteria such as user input. For example:
using System;
using static System.Console;
private static bool exit = false;
private static string serverName = string.Empty;
static void Main(string[] args) {
WriteLine("Please enter a command.");
string response = ReadLine();
switch (response) {
case "setserver": SetServer(); break;
case "changepass": ChangePassword(); break;
}
}
ReadKey();
}
static void SetServer() {
WriteLine("Please enter a server name.");
serverName = ReadLine(); // You should probably validate the user input here.
}
static void ChangePassword() {
// Execute your needed password change code here.
}
This will get you started with executing multiple commands in a console application.
Changing a Password (Active Directory)
For Active Directory changes, you should take a look at this post and this documentation for more information. The code it utilizes (in case the link ever dies for some reason) is:
// Connect to Active Directory and get the DirectoryEntry object.
// Note, ADPath is an Active Directory path pointing to a user. You would have created this
// path by calling a GetUser() function, which searches AD for the specified user
// and returns its DirectoryEntry object or path. See http://www.primaryobjects.com/CMS/Article61.aspx
DirectoryEntry oDE;
oDE = new DirectoryEntry(ADPath, ADUser, ADPassword, AuthenticationTypes.Secure);
try {
// Change the password.
oDE.Invoke("ChangePassword", new object[]{strOldPassword, strNewPassword});
}
catch (Exception e) {
Debug.WriteLine($"Error changing password. Reason: {e.Message}");
}
Changing a Password (SQL Server)
For SQL Server changes, I would recommend you look into this post (where there are two decent answers to solving this issue) and this documentation on changing the password at the SqlConnection
object level.
SqlConnection.ChangePassword(string, string);
Changes the SQL Server password for the user indicated in the connection string to the supplied new password.
The answers provided to the other post would combine this code:
string sqlquery = "SELECT Password FROM [Member] where Username=@username";
SqlCommand cmd = new SqlCommand(sqlquery, connect);
cmd.Parameters.AddWithValue("@username", label_username.Text);
cmd.Connection = connect;
string currentPassword = (string)cmd.ExecuteScalar();
if (currentPassword == textBox_Current.Text) {
// PASSWORD IS CORRECT, CHANGE IT, NOW.
} else {
// WOW EASY BUDDY, NOT SO FAST
}
With this advice:
- consider your string username -> Hash it -> write a query to check whether that hashed value and the user's password's hash value stored in the database is the same
- consider string password and string newPassword -> check whether each is null or the length is 0
- consider string password and string newPassword in your code -> Hash both -> check whether the hash values are the same
An Important Note
No matter what issue you are trying to solve, when dealing with passwords you should put heavy thought into the hashing and encrypting that comes along with passwords. If you're going to change a password on a server, you should also verify the old password prior to making changes. You should also validate the new password by checking for illegal characters, ensuring the password strength is adequate, verifying the initial entry of the new password matches a confirmation entry, among a few other things such as the hashing and encryption that may be surrounding the password you'll need to verify against.
Thorough Posting
If you need a more thorough answer to a more specific issue such as actually changing the password on a server you have permissions to, then update your question and be more thorough by following mjwills' advice and visiting the Minimal, Complete, and Verifiable example. This will allow other readers not only assist you in a better manner, but will also assist future readers with similar issues. Once you have updated your question to reflect the truly desired result, I will update my answer (if I have the answer) to demonstrate the knowledge you're seeking.