代码之家  ›  专栏  ›  技术社区  ›  VA systems engineer

如何使用安全字符串密码从C程序登录到Xen会话?

  •  1
  • VA systems engineer  · 技术社区  · 7 年前

    我正在使用PowerShell 5.1、Visual Studio 2017、C_和XenServer SDK 7.1.1。

    在PowerShell程序中使用get credentials和export clixml,我已将根用户的池主服务器登录凭据保存到XML凭据文件(xml_creds.xml)

    现在,我想使用C_创建和登录会话(请参见下面的代码)。如您所见,我被迫将安全字符串转换为纯文本字符串以满足Xen.NET API的签名。 login_with_password 方法。

    使用API,如何使用安全字符串登录会话?

    代码

    try
    {
    
        securedPassword = new SecureString();
        string unsecuredPassword = "";
    
        Runspace rs = RunspaceFactory.CreateRunspace();
        rs.Open();
    
        Pipeline pipeline = rs.CreatePipeline(@"Import-CliXml 'C:\foo\xml_creds.xml';");
    
        Collection<PSObject> results = pipeline.Invoke();
    
        if (results.Count == 1)
        {
            PSObject psOutput = results[0];
    
            securedPassword = ((PSCredential)psOutput.BaseObject).Password;
            unsecuredPassword = new System.Net.NetworkCredential(string.Empty, securedPassword).Password;
            username = ((PSCredential)psOutput.BaseObject).UserName;
    
            rs.Close();
    
            session = new Session(hostname, port);
    
            session.login_with_password(username, unsecuredPassword, API_Version.API_1_3);
        }
        else
        {
            throw new System.Exception("Could not obtain pool master server credentials");
        }
    }
    catch (Exception e1)
    {
        System.Console.WriteLine(e1.Message);
    }
    finally
    {
        if (securedPassword != null)
        {
            securedPassword.Dispose();
        }
    
        if (session != null)
        {
            session.logout(session);
        }
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   VA systems engineer    7 年前

    我联系了Citrix。

    The Xen API does not provide a mechanism for logging into a session using a secure string password .

    所以,我最终使用了一个执行两个 PowerShell 支持安全字符串密码的脚本。

    请参见下面的代码。

    笔记:

    我安装了7.1.1 XenServerSModule %userprofile%\documents\windowspowershell\modules\xenserversmodule。此模块提供Connect-XenServer cmdlet

    我使用PowerShell获取凭据创建了XML凭据文件 然后使用export clixml

    form1.cs(表单只有一个按钮)

    using System;
    using System.Windows.Forms;
    using XenSnapshotsXenAccess;
    
    namespace Create_XenSnapshotsUi
    {
        public partial class Form1 : Form
        {
            XenSessionAccess xenSession = null;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Button1_Click(object sender, EventArgs e)
            {
    
                xenSession = new XenSessionAccess("https://xxx.xx.x.x", @"C:\foo\xml_credentials.xml");
    
                xenSession.Logout();
    
            }
        }
    }
    

    XensessionAccess类

    using System;
    using System.Collections.ObjectModel;
    using System.Management.Automation;
    using System.Management.Automation.Runspaces;
    using XenAPI;
    
    namespace XenSnapshotsXenAccess
    {
        public class XenSessionAccess
        {
            private Session xenSession = null;
    
            public Session XenSession { get => xenSession; set => xenSession = value; }
    
            public void Logout()
            {
                if (XenSession != null)
                {
                    XenSession.logout(XenSession);
                }
            }
    
            public XenSessionAccess(string poolMasterServerUrl, string xml_creds_path)
            {
                Collection<PSObject> results = null;
                PSCredential psCredential = null;
    
                //https://docs.microsoft.com/en-us/powershell/developer/hosting/creating-an-initialsessionstate
    
                //Createdefault2* loads only the commands required to host Windows PowerShell (the commands from the Microsoft.PowerShell.Core module.
                InitialSessionState initialSessionState = InitialSessionState.CreateDefault2();
    
                using (Runspace runSpace = RunspaceFactory.CreateRunspace(initialSessionState))
                {
                    runSpace.Open();
    
                    using (PowerShell powerShell = PowerShell.Create())
                    {
                        powerShell.Runspace = runSpace;
                        powerShell.AddCommand("Import-CliXml");
    
                        powerShell.AddArgument(xml_creds_path);
                        results = powerShell.Invoke();
    
                        if (results.Count == 1)
                        {
                            PSObject psOutput = results[0];
                            //cast the result to a PSCredential object
                            psCredential = (PSCredential)psOutput.BaseObject;
                        }
                        else
                        {
                            throw new System.Exception("Could not obtain pool master server credentials");
                        }
                    }
    
                    runSpace.Close();
                }
    
                initialSessionState = InitialSessionState.CreateDefault2();
                initialSessionState.ImportPSModule(new string[] { "XenServerPSModule" });
                initialSessionState.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Unrestricted;
    
                SessionStateVariableEntry psCredential_var = new SessionStateVariableEntry("psCredential", psCredential, "Credentials to log into pool master server");
                initialSessionState.Variables.Add(psCredential_var);
    
                SessionStateVariableEntry poolUrl_var = new SessionStateVariableEntry("poolUrl", poolMasterServerUrl, "Url of pool master server");
                initialSessionState.Variables.Add(poolUrl_var);
    
                using (Runspace runSpace = RunspaceFactory.CreateRunspace(initialSessionState))
                {
                    runSpace.Open();
    
                    using (PowerShell powerShell = PowerShell.Create())
                    {
                        powerShell.Runspace = runSpace;
                        powerShell.AddScript(@"$psCredential | Connect-XenServer -url $poolUrl -SetDefaultSession -PassThru");
                        results = powerShell.Invoke();
                    }
    
                    if (results.Count == 1)
                    {
                        PSObject psOutput = results[0];
                        //cast the result to a XenAPI.Session object
                        XenSession = (Session)psOutput.BaseObject;
                    }
                    else
                    {
                        throw new System.Exception(String.Format("Could not create session for {0}", poolMasterServerUrl));
                    }
    
                    runSpace.Close();
                }
            }
        }
    }
    
    推荐文章