How do you authenticate a user and pswd for local accounts from windowsforms?

M

Michael Howes

I would think this would be very, very easy but in the 50 searches
I've done I haven't found anything.

If our application requires login and that user/password be a local
windows account or more detailed, a user that has been added to the
Power Users group that is either a local account or a active directory
account how do I authenticate?

I've found code that seems to do this against Active Directory

public static bool LogonValid(string userName, string password) {
DirectoryEntry de = new DirectoryEntry(null, domain + "\\" +
userName, password);
try {
object o = de.NativeObject;
DirectorySearcher ds = new DirectorySearcher(de);
ds.Filter = "samaccountname=" + userName;
ds.PropertiesToLoad.Add("cn");
SearchResult sr = ds.FindOne();
if(sr == null) throw new Exception();
return true;
} catch {
return false;
}
}

but this doesn't work for local accounts.

how does one authenticate against local accounts in a c#/windows forms
application?

thanks
mike
 
T

Tom Spink

Michael said:
I would think this would be very, very easy but in the 50 searches
I've done I haven't found anything.

If our application requires login and that user/password be a local
windows account or more detailed, a user that has been added to the
Power Users group that is either a local account or a active directory
account how do I authenticate?

I've found code that seems to do this against Active Directory

public static bool LogonValid(string userName, string password) {
DirectoryEntry de = new DirectoryEntry(null, domain + "\\" +
userName, password);
try {
object o = de.NativeObject;
DirectorySearcher ds = new DirectorySearcher(de);
ds.Filter = "samaccountname=" + userName;
ds.PropertiesToLoad.Add("cn");
SearchResult sr = ds.FindOne();
if(sr == null) throw new Exception();
return true;
} catch {
return false;
}
}

but this doesn't work for local accounts.

how does one authenticate against local accounts in a c#/windows forms
application?

thanks
mike

Hi Mike,

I believe the API you're looking for is LogonUser. Local windows accounts
aren't part of an AD system.

Take a look at this page to get started:
http://www.codeproject.com/useritems/User_Impersonation_in_Ne.asp

This example shows how to impersonate a user, but it contains an example of
using the LogonUser API in managed code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top