ASP.NET(C#)修改FTP密碼

來源:互聯網
上載者:User

用asp.net修改FTP密碼
FTP開通後一般會有一個分配好的密碼,如果使用者想修改這個密碼可以用命令的方式來修改。但並不是每個人都懂得如何使用命令,如果可以提供一個網頁,可以讓使用者自己修改的話那就方便多了。以下的代碼主要是通過.net調用微軟封裝好的wininet.dll動態串連庫裡面的一些方法來現實修改密碼的,這個類還封裝了其它的方法,可以應用於其它的網路服務。這些例子也可以做為.net裡如何調用windows API函數的例子

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(
        [MarshalAs(UnmanagedType.LPTStr)] string agent,
        InternetOpenType accessType,
        [MarshalAs(UnmanagedType.LPTStr)] string lpszProxyName,
        [MarshalAs(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,
        [MarshalAs(UnmanagedType.LPTStr)] string lpszCommand,
        IntPtr context,
        IntPtr phFtpCommand
        );

    [DllImport("WinInet.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern IntPtr InternetConnect(
       IntPtr hInternet,
       [MarshalAs(UnmanagedType.LPTStr)] string serverName,
       InternetPort serverPort,
       [MarshalAs(UnmanagedType.LPTStr)] string username,
       [MarshalAs(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 = "不能開啟串連!";
        }
        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 = "密碼修改成功!";
                }
                else
                {
                    msg = "密碼修改失敗!";
                }
            }
            else
            {
                msg = "不能登陸FTP伺服器,請檢查使用者名稱與密碼是否正確!";
            }
        }
        if (connection != (IntPtr)0)
        {
            InternetCloseHandle(connection);
        }
        if (open != (IntPtr)0)
        {
            InternetCloseHandle(open);
        }
        return msg;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string msg = ChangePassword(userName.Text, oldPassword.Text, newPassword.Text);
        Response.Write(msg);
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.