繁体   English   中英

使用 PowerShell cmdlet 使用加密密码的密钥连接到 C# 中的 Exchange Online

[英]Connect to Exchange Online in C# with PowerShell cmdlet using a key on encrypted password

我想从 C# 在线访问 Exchange,例如提供管理灵活性的 Web 应用程序。 通常,我使用下面的 powershell 脚本。

//the key and password was changed

IF ($session.state -ne 'Opened') {
    # encryption key
    $key = (3,4,12,3,56,34,211,22,1,1,22,23,42,54,33,233,81,34,2,27,116,5,35,43)

    $adminUser = "admin@domain.onmicrosoft.com" 
    $adminPwd = "76492d1974683f0423413b12570a5345MgB8AGgAZgB0AE4AeABlAG4AbgB5AHYATABwAE4AbwB5AGgAUABtAHoAbwBYAEEAPQA9AHhn208edA8nE973VsDji2wAMwA3AGMAYQBkADgAMgA2ADYAZABkADIAMQA1AGEAMQBiAGQAOQAQBiADEANAAyAGEANAA=" | ConvertTo-SecureString -Key $Key
    $psCred = New-Object System.Management.Automation.PsCredential $adminUser,$adminPwd 

    $O365Url = "https://outlook.office365.com/powershell-liveid/" 

    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $O365Url -Credential $psCred -Authentication basic -AllowRedirection 

    Import-PSSession $Session -AllowClobber
}
Else {
    Write-Host 'Opened session'

}

// do something

Remove-PSSession $Session -AllowClobber

现在我尝试在 C# 中做同样的事情。 当密码为明文时,我可以连接。 我不明白如何包含我的加密密钥。 这里的代码:

byte[] key = {3,4,12,3,56,34,211,22,1,1,22,23,42,54,33,233,81,34,2,27,116,5,35,43};

string adminUser = "admin@domain.onmicrosoft.com" 
string adminPwd = "76492d1974683f0423413b12570a5345MgB8AGgAZgB0AE4AeABlAG4AbgB5AHYATABwAE4AbwB5AGgAUABtAHoAbwBYAEEAPQA9AHhn208edA8nE973VsDji2wAMwA3AGMAYQBkADgAMgA2ADYAZABkADIAMQA1AGEAMQBiAGQAOQAQBiADEANAAyAGEANAA=";
string O365Url = "https://outlook.office365.com/powershell-liveid/";

SecureString adminPwdSecure = new SecureString();
PSObject SessionHolder = null;

foreach (char c in adminPwd.ToCharArray())
    adminPwdSecure.AppendChar(c);

adminPwdSecure.MakeReadOnly();

PSCredential credential = new PSCredential(adminUser, adminPwdSecure);

Runspace runspace = RunspaceFactory.CreateRunspace();
PowerShell powershell = PowerShell.Create();

PSCommand command = new PSCommand();
command.AddCommand("New-PSSession");
command.AddParameter("ConfigurationName", "Microsoft.Exchange");
command.AddParameter("ConnectionUri", new Uri(O365Url));
command.AddParameter("Credential", credential);
command.AddParameter("Authentication", "Basic");
powershell.Commands = command;

runspace.Open();
powershell.Runspace = runspace;
Collection<PSObject> result = powershell.Invoke();
if (powershell.Streams.Error.Count > 0 || result.Count != 1)
{
    throw new Exception("Fail to establish the connection");
}
else
{
    //Success to establish the connection
    SessionHolder = result[0];
}

PSCommand ImportSession = new PSCommand();
ImportSession.AddCommand("Import-PSSession");
ImportSession.AddParameter("Session", SessionHolder);
powershell.Commands = ImportSession;
powershell.Invoke();

// do something

PSCommand RemoveSession = new PSCommand();
RemoveSession.AddCommand("Remove-PSSession");
RemoveSession.AddParameter("Session", SessionHolder);
powershell.Commands = RemoveSession;
powershell.Invoke();

您可以尝试像这样创建加密密钥:

    byte[] key = { 3, 4, 12, 3, 56, 34, 211, 22, 1, 1, 22, 23, 42, 54, 33, 233, 81, 34, 2, 27, 116, 5, 35, 43 };

    string adminUser = "admin@domain.onmicrosoft.com";
    string adminPwd = "76492d1974683f0423413b12570a5345MgB8AGgAZgB0AE4AeABlAG4AbgB5AHYATABwAE4AbwB5AGgAUABtAHoAbwBYAEEAPQA9AHhn208edA8nE973VsDji2wAMwA3AGMAYQBkADgAMgA2ADYAZABkADIAMQA1AGEAMQBiAGQAOQAQBiADEANAAyAGEANAA=";
    string O365Url = "https://outlook.office365.com/powershell-liveid/";

    PSObject SessionHolder = null;

    Runspace runspace = RunspaceFactory.CreateRunspace();
    PowerShell powershell = PowerShell.Create();

    runspace.Open();
    powershell.Runspace = runspace;

    // >>> Create $adminPwdSecure encrypted variable using PowerShell:
    powershell.AddCommand("ConvertTo-SecureString")
        .AddParameter("String", adminPwd)
        .AddParameter("Key", key)
        .AddCommand("New-Variable")
        .AddParameter("Name", "adminPwdSecure")
        .Invoke();

    // >>> Read $adminPwdSecure variable from session state:
PSCredential credential = new PSCredential(adminUser, (SecureString)((PSObject)pwsh.Runspace.SessionStateProxy.PSVariable.GetValue("adminPwdSecure")).BaseObject);

    PSCommand command = new PSCommand();
    command.AddCommand("New-PSSession");
    command.AddParameter("ConfigurationName", "Microsoft.Exchange");
    command.AddParameter("ConnectionUri", new Uri(O365Url));
    command.AddParameter("Credential", credential);
    command.AddParameter("Authentication", "Basic");
    powershell.Commands = command;

    Collection<PSObject> result = powershell.Invoke();
    if (powershell.Streams.Error.Count > 0 || result.Count != 1)
    {
        throw new Exception("Fail to establish the connection");
    }
    else
    {
        //Success to establish the connection
        SessionHolder = result[0];
    }

    PSCommand ImportSession = new PSCommand();
    ImportSession.AddCommand("Import-PSSession");
    ImportSession.AddParameter("Session", SessionHolder);
    powershell.Commands = ImportSession;
    powershell.Invoke();

    // do something

    PSCommand RemoveSession = new PSCommand();
    RemoveSession.AddCommand("Remove-PSSession");
    RemoveSession.AddParameter("Session", SessionHolder);
    powershell.Commands = RemoveSession;
    powershell.Invoke();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM