LogonUser API

D

Dan

All,

I am attempting to use the LogonUser API in an
application. However, everytime I attempt to validate an
account using this I get an error. The code is 1421 which
has a description of "Control ID not found". I can't find
any information on what could be causing this error. Any
help would be appreciated. Thanks!
 
W

Willy Denoyette [MVP]

Error 1421 is not related to LogonUser, could you please post your code, or a small repro?

Willy.
 
D

Dan

Thanks for the response. Here is the code

public class LogonAPI
{


// Declare the logon types as constants
const long LOGON32_LOGON_INTERACTIVE = 2;
const long LOGON32_LOGON_NETWORK = 3;

// Declare the logon providers as constants
const long LOGON32_PROVIDER_DEFAULT = 0;
const long LOGON32_PROVIDER_WINNT50 = 3;
const long LOGON32_PROVIDER_WINNT40 = 2;
const long LOGON32_PROVIDER_WINNT35 = 1;

//Import Needed DLL
[DllImport("advapi32.dll",EntryPoint
= "LogonUser")]
private static extern bool LogonUser(

string lpszUsername,

string lpszDomain,

string lpszPassword,

int dwLogonType,

int dwLogonProvider,

ref IntPtr phToken);


public LogonAPI()
{
}

public bool ValidateLogin( string
Username, string Password, string Domain )
{


IntPtr token = new IntPtr(0);
token = IntPtr.Zero;

// Call the API
if (LogonUser(
Username,
Domain,
Password,
(int)
LOGON32_LOGON_NETWORK,
(int)
LOGON32_PROVIDER_DEFAULT,
ref token))
{
return true;
}
else
{
return false;
}
}

}

When I attempt to call the ValidateLogin function I always
get a return value of false. I then use the
System.Runtime.InteropServices.Marshal.GetLastWin32Error()
function to get the error and it returns 1421.

Dan
-----Original Message-----
Error 1421 is not related to LogonUser, could you please
post your code, or a small repro?
 
W

Willy Denoyette [MVP]

- Why const long for logontypes and provider constants, change them to int so no cast is needed when calling LogonUser.
- Add SetLastError=true to your DllImport declaration.

Willy.

Dan said:
Thanks for the response. Here is the code

public class LogonAPI
{


// Declare the logon types as constants
const long LOGON32_LOGON_INTERACTIVE = 2;
const long LOGON32_LOGON_NETWORK = 3;

// Declare the logon providers as constants
const long LOGON32_PROVIDER_DEFAULT = 0;
const long LOGON32_PROVIDER_WINNT50 = 3;
const long LOGON32_PROVIDER_WINNT40 = 2;
const long LOGON32_PROVIDER_WINNT35 = 1;

//Import Needed DLL
[DllImport("advapi32.dll",EntryPoint
= "LogonUser")]
private static extern bool LogonUser(

string lpszUsername,

string lpszDomain,

string lpszPassword,

int dwLogonType,

int dwLogonProvider,

ref IntPtr phToken);


public LogonAPI()
{
}

public bool ValidateLogin( string
Username, string Password, string Domain )
{


IntPtr token = new IntPtr(0);
token = IntPtr.Zero;

// Call the API
if (LogonUser(
Username,
Domain,
Password,
(int)
LOGON32_LOGON_NETWORK,
(int)
LOGON32_PROVIDER_DEFAULT,
ref token))
{
return true;
}
else
{
return false;
}
}

}

When I attempt to call the ValidateLogin function I always
get a return value of false. I then use the
System.Runtime.InteropServices.Marshal.GetLastWin32Error()
function to get the error and it returns 1421.

Dan
-----Original Message-----
Error 1421 is not related to LogonUser, could you please
post your code, or a small repro?
 

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