Use Asp.net to change the FTP Password
After FTP is activated, a password is usually assigned. If you want to change the password, you can use a command to change it. However, not everyone knows how to use commands. If you can provide a webpage that allows users to modify it by themselves, it is much more convenient. The following code mainly uses. net calls Microsoft encapsulated wininet. some methods in the DLL dynamic Connection Library are used to change the password. This class also encapsulates other methods and can be applied to other network services. These examples can also be used as examples of how to call Windows API functions in. net.
Using system;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. runtime. interopservices;
Public partial class _ default: system. Web. UI. Page
{
Public Enum internetopentype
{
Preconfig = 0,
Direct = 1,
Proxy = 3,
Preconfigwithnoautoproxy = 4,
}
Public Enum internetport
{
Invalidportnumber = 0,
Defaultftpport = 21,
Defaultgopherport = 70,
Defaulthttpport = 80,
Defaulthttpsport = 443,
Defaultsocksport = 1080,
}
Public Enum internetservice
{
FTP = 1,
Gopher = 2,
HTTP = 3,
}
[Dllimport ("wininet. dll", charset = charset. Auto, setlasterror = true)]
Public static extern intptr internetopen (
[Financialas (unmanagedtype. lptstr)] string agent,
Internetopentype accesstype,
[Financialas (unmanagedtype. lptstr)] string lpszproxyname,
[Financialas (unmanagedtype. lptstr)] string lpszproxybypass,
Int flags
);
[Dllimport ("wininet. dll", charset = charset. Auto, setlasterror = true)]
Private Static extern bool ftpcommand (
Intptr hconnect,
Bool expectresponse,
Intptr dwflags,
[Financialas (unmanagedtype. lptstr)] string lpszcommand,
Intptr context,
Intptr phftpcommand
);
[Dllimport ("wininet. dll", charset = charset. Auto, setlasterror = true)]
Public static extern intptr internetconnect (
Intptr hinternet,
[Financialas (unmanagedtype. lptstr)] string servername,
Internetport SERVERPORT,
[Financialas (unmanagedtype. lptstr)] string username,
[Financialas (unmanagedtype. lptstr)] string password,
Internetservice service,
Int flags,
Intptr Context
);
[Dllimport ("wininet. dll", setlasterror = true)]
Public static extern bool internetclosehandle (intptr hinternet );
Protected void page_load (Object sender, eventargs E)
{
}
Private string changepassword (string username, string oldpassword, string newpassword)
{
Intptr open = (intptr) 0;
Intptr connection = (intptr) 0;
String strcmd;
String MSG;
Strcmd = "site pswd" + oldpassword + "" + newpassword;
Open = internetopen ("ftpconnection", internetopentype. Direct, null, null, 0 );
If (open = (intptr) 0)
{
MSG = "the connection cannot be opened! ";
}
Else
{
Connection = internetconnect (open, "ftp.yourserv.com.cn", internetport. defaultftpport, username, oldpassword, internetservice. FTP, 0, intptr. Zero );
If (connection! = (Intptr) 0)
{
Bool result = ftpcommand (connection, false, (intptr) 1, strcmd, (intptr) 0, (intptr) 0 );
If (result)
{
MSG = "password changed! ";
}
Else
{
MSG = "An error occurred while changing the password! ";
}
}
Else
{
MSG = "You cannot log on to the FTP server. Check whether the user name and password are correct! ";
}
}
If (connection! = (Intptr) 0)
{
Internetclosehandle (connection );
}
If (open! = (Intptr) 0)
{
Internetclosehandle (open );
}
Return MSG;
}
Protected void button#click (Object sender, eventargs E)
{
String MSG = changepassword (username. Text, oldpassword. Text, newpassword. Text );
Response. Write (MSG );
}
}