.net 使用ASP会话变量创建WinSCP SessionOptions,使其可在多个方法之间重用

cnwbcb6i  于 2022-12-14  发布在  .NET
关注(0)|答案(1)|浏览(139)

我在C# ASP页面中创建WinSCP Session[Options]变量时遇到问题。
我有登录页面,我正在那里定义会话变量.

namespace HLR_Handling
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["sftpHostName"] = "x.x.x.x";
            Session["sftpUserName"] = "tempuser";  
        }
    }
}

Profile_Search页面中,我尝试将该变量值分配给SessionOption参数,但在此出现以下错误。
CS0120:sessionoptions的非静态字段、方法或属性“System.Web.UI.Page.Session.get”需要对象引用

public partial class Profile_Search : System.Web.UI.Page
{
    SessionOptions sessionOptions = new SessionOptions
    {
        Protocol = Protocol.Sftp,
        HostName = Session["sftpHostName"].ToSTring(),
        UserName = Session["sftpUserName"],
        Password = "xxxxxx",
        PortNumber = 22,
        SshHostKeyFingerprint = "ssh-rsa x:x:X:Xxxxxxxx"
    };

    protected void btnHLR_Click(object sender, EventArgs e)
    {
        using (Session session = new Session())
        {
            // Connect
            session.Open(sessionOptions);

            // ....        
        }
    }
}
oxiaedzo

oxiaedzo1#

SessionOptions声明移到方法中。

protected void btnHLR_Click(object sender, EventArgs e)
{
    using (Session session = new Session())
    {
        SessionOptions sessionOptions = new SessionOptions
        {
            Protocol = Protocol.Sftp,
            HostName = Session["sftpHostName"].ToSTring(),
            UserName = Session["sftpUserName"],
            Password = "xxxxxx",
            PortNumber = 22,
            SshHostKeyFingerprint = "ssh-rsa x:x:X:Xxxxxxxx"
        };

        // Connect
        session.Open(sessionOptions);

        // ...
    }
}

如果需要重复使用会话选项,可以将创建分解为一个方法:

protected SessionOptions CreateSessionOptions()
{
    return new SessionOptions
    {
        Protocol = Protocol.Sftp,
        HostName = Session["sftpHostName"].ToSTring(),
        UserName = Session["sftpUserName"],
        Password = "xxxxxx",
        PortNumber = 22,
        SshHostKeyFingerprint = "ssh-rsa x:x:X:Xxxxxxxx"
    };
}

protected void btnHLR_Click(object sender, EventArgs e)
{
    using (Session session = new Session())
    {
        // Connect
        session.Open(CreateSessionOptions());

        // ...
    }
}

或者,您可以将整个会话创建分解为:

protected Session CreateSession()
{
    Session session = new Session();
    try
    {
        SessionOptions sessionOptions = new SessionOptions
        {
            Protocol = Protocol.Sftp,
            HostName = Session["sftpHostName"].ToSTring(),
            UserName = Session["sftpUserName"],
            Password = "xxxxxx",
            PortNumber = 22,
            SshHostKeyFingerprint = "ssh-rsa x:x:X:Xxxxxxxx"
        };

        // Connect
        session.Open(sessionOptions);
    }
    catch
    {
        session.Dispose();
        throw;
    }
    return session;
}

protected void btnHLR_Click(object sender, EventArgs e)
{
    using (Session session = CreateSession())
    {
        // ...
    }
}

相关问题