1. There is a problem with the function parameter selection, which is inconsistent with your own environment.
For example
Public static extern int logonuser (string lpszusername,
String lpszdomain,
String lpszpassword,
Int dwlogontype,
Int dwlogonprovider,
Ref intptr phtoken );
To access remote resources, use logon32_logon_new_credentials,
Logon32_logon_interactive is required to simulate a local user.
2. The parameter format of the function is incorrect.
A. For example
Public static extern int logonuser (string lpszusername,
String lpszdomain,
String lpszpassword,
Int dwlogontype,
Int dwlogonprovider,
Ref intptr phtoken );
The lpszusername, lpszdomain, and lpszpassword in must be clearly written.
I encountered a problem here. During the first test, the remote server is an independent file server. This is my call method:
Logonuser ("myname", "192.168.1.48", "password", logon32_logon_new_credentials,
Logon32_provider_default, ref token );
During the second test, the remote server is a member server in the domain mydomain and provides file services. The Code should be:
Logonuser ("myname", "mydomain", "password", logon32_logon_new_credentials,
Logon32_provider_default, ref token );
Note that the code is mydomain rather than IP address.
B. For example:
Refer to the above Code
String remote = @ "\ 192.168.1.48 \ generals ";
String local = @ "P :";
String username = @ "domain \ Username ";
String Password = @ "password ";
If @ "\ 192.168.1.48 \ generals" is changed to @ "\ 192.168.1.48 \ generals \", an error occurs;
If it is a user in the domain, an error occurs when @ "domain \ Username" is changed to @ "username ".
--/////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////
Public Enum logontype
{
/// <Summary>
/// This logon type is intended for users who will be interactively using the computer, such as a user being logged on
/// By a terminal server, remote shell, or similar process.
/// This logon type has the additional expense of caching logon information for disconnected operations;
/// Therefore, it is inappropriate for some client/server applications,
/// Such as a mail server.
/// </Summary>
Logon32_logon_interactive = 2,
/// <Summary>
/// This logon type is intended for High Performance servers to authenticate plaintext passwords.
/// The logonuser function does not cache credentials for this logon type.
/// </Summary>
Logon32_logon_network = 3,
/// <Summary>
/// This logon type is intended for batch servers, where processes may be executing on behalf of a user
/// Their direct intervention. This type is also for higher performance servers that process implements plaintext
/// Authentication attempts at a time, such as mail or web servers.
/// The logonuser function does not cache credentials for this logon type.
/// </Summary>
Logon32_logon_batch = 4,
/// <Summary>
/// Indicates a service-type logon. The account provided must have the Service Privilege enabled.
/// </Summary>
Logon32_logon_service = 5,
/// <Summary>
/// This logon type is for Gina DLLs that log on users who will be interactively using the computer.
/// This logon type can generate a unique audit record that shows when the workstation was unlocked.
/// </Summary>
Logon32_logon_unlock = 7,
/// <Summary>
/// This logon type preserves the name and password in the authentication package, which allows the server to make
/// Connections to other network servers while impersonating the client. A server can accept plaintext credentials
/// From a client, call logonuser, verify that the user can access the system authentication ss the network, and still
/// Communicate with other servers.
/// Note: Windows NT: this value is not supported.
/// </Summary>
Logon32_logon_network_cleartext = 8,
/// <Summary>
/// This logon type allows the caller to clone its current token and specify new credentials for outbound connections.
/// The new logon session has the same local identifier but uses different credentials for other network connections.
/// Note: This logon type is supported only by the logon32_provider_winnt50 logon provider.
/// Note: Windows NT: this value is not supported.
/// </Summary>
Logon32_logon_new_credentials = 9,
}
Public Enum logonprovider
{
/// <Summary>
/// Use the standard Logon provider for the system.
/// The default security provider is negotiate, unless you pass NULL for the domain name and the user name
/// Is not in UPN format. In this case, the default provider is NTLM.
/// Note: Windows 2000/NT: the default security provider is NTLM.
/// </Summary>
Logon32_provider_default = 0,
}
--/////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////
Const int logon32_logon_interactive = 2;
Const int logon32_logon_network = 3;
Const int logon32_logon_batch = 4;
Const int logon32_logon_service = 5;
Const int logon32_logon_unlock = 7;
Const int logon32_logon_network_cleartext = 8;
Const int logon32_logon_new_credentials = 9;
Const int logon32_provider_default = 0;