Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using windows username password in C#

how i can use windows stored username and password in my C# APP. I am looking for method which returns bool state of successful login. means :

bool isCredentialValid = CheckLogin(username, password);

so according to bool value I can show proper message.

Updated

class Program
{
    static void Main(string[] args)
    {
        PrincipalContext pc = new PrincipalContext(ContextType.Machine);
        Console.WriteLine("Enter username : ");
        string user = Console.ReadLine();

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

        bool isValid = pc.ValidateCredentials(user, pass);

        Console.WriteLine("State : {0}",isValid);

        Console.ReadLine();
    }
}

this code is not working. when i enable Guest account it shows true for every login and when i disable Guest account it does not verifies even existing account. Any help would be appreciated.

like image 387
Raj Avatar asked Dec 21 '22 19:12

Raj


1 Answers

Have a look at PrincipalContext.ValidateCredentials method. For example,

PrincipalContext pc = new PrincipalContext(ContextType.Domain);
bool isCredentialValid = pc.ValidateCredentials(username, password);

for local accounts, use ContextType.Machine.

Yet another way would be using win32 api LogonUser function

like image 160
VinayC Avatar answered Jan 08 '23 14:01

VinayC