Click here to Skip to main content
15,887,336 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I want to create a code using netuser function that will search by clicking on the button:
C#
private void NextForm3_Click(object sender, EventArgs e)
if the user by selecting an Item from listbox.
C#
private void ExistUserList_SelectedIndexChanged(object sender, EventArgs e)

is in logged in computer, in Windows services. If it has an account second of all, it must search if it has the correct password from textbox:
C#
private void FirstPassword_TextChanged(object sender, EventArgs e)

to computer, in Windows services. When both things are correct, the user can go to the next dialogbox.

What I have tried:

Whenever I write a random password to the selected user in listbox, I have access to go to from Form2.cs to Form3.cs. This is my selected, shorter version of my code:
C#
private void NextForm3_Click(object sender, EventArgs e)
{
    string selectedUsername = Username.Text; // Pobierz nazwę użytkownika z TextBox
    string enteredPassword = FirstPassword.Text; // Pobierz hasło z TextBox

    // Sprawdź, czy użytkownik istnieje w systemie
    bool userExists = CheckIfUserExists(selectedUsername);

    if (userExists)
    {
        // Użytkownik istnieje w systemie
        // Teraz sprawdź, czy hasło jest poprawne
        bool passwordIsCorrect = CheckIfPasswordIsCorrect
                                 (selectedUsername, enteredPassword);

        if (passwordIsCorrect)
        {
            // Hasło jest poprawne, możesz kontynuować
            AllForm2 = AllText2.Text;

            // Dodaj kod do przejścia do kolejnego formularza (Form3) w tym miejscu
            Form3 next3 = new Form3();
            next3.Show();
            this.Close();
        }
        else
        {
            // Hasło jest niepoprawne
            // Dodaj kod obsługujący niepoprawne hasło
        }
    }
    else
    {
        // Użytkownik nie istnieje w systemie
        // Dodaj kod obsługujący brak użytkownika
    }
}

private bool CheckIfUserExists(string username)
{
    // Użyj procesu, aby uruchomić "net user" i sprawdzić, czy użytkownik istnieje
    Process process = new Process();
    process.StartInfo.FileName = "net";
    process.StartInfo.Arguments = "user " + username;
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.CreateNoWindow = true;

    process.Start();
    string output = process.StandardOutput.ReadToEnd();
    process.WaitForExit();

    // Sprawdź, czy użytkownik istnieje w wynikach
    return output.Contains(username);
}

private bool CheckIfPasswordIsCorrect(string username, string password)
{
    // Użyj procesu, aby uruchomić "net user" i sprawdzić, 
    // czy hasło jest poprawne
    Process process = new Process();
    process.StartInfo.FileName = "net";
    process.StartInfo.Arguments = "user " + username + " " + password;
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.CreateNoWindow = true;

    process.Start();
    string output = process.StandardOutput.ReadToEnd();
    process.WaitForExit();

    // Sprawdź, czy hasło jest poprawne w wynikach
    return !output.Contains("The user name could not be found");
}
Posted
Updated 4-Nov-23 1:54am
v2
Comments
Richard Deeming 3-Nov-23 10:49am    
Dave has already told you how to do that:
Visual studio 2017 windows forms C# - password from lusrmgr.mfc[^]
Inczu 3-Nov-23 11:03am    
But now I dont want to put the password automatic from lusrmgr.msc or some Windows services in the textbox. I just want make sure that the selected user from listbox is in Windows services and selected user is write the correct password to textbox from Windows services.
Richard Deeming 3-Nov-23 11:46am    
Read Dave's solution again - specifically the last part, starting "You can use the PrincipleContext class to validate credentials ..."

1 solution

Like Richard already said, I told you how to validate credentials against the machines local account database that does not use shelling out to "NET USE".

It is not called "in Windows services". That phrase means something completely different from what you're talking about.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900