I used to use Basic authentication when connecting to Exchange online, but after Microsoft disabled the basic authentication I have not had luck connecting to it. Having it use authentication popup would be ideal.
Code looked something like this:
PSCredential credential = new PSCredential(username, new NetworkCredential("", password).SecurePassword);
WSManConnectionInfo wsEOConnInfo = new WSManConnectionInfo((new Uri("https://outlook.office365.com/powershell-liveid/")),
"http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
wsEOConnInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
try
{
using (Runspace runspace = RunspaceFactory.CreateRunspace(wsEOConnInfo))
{
runspace.Open();
try
{
if (runspace.RunspaceStateInfo.State == RunspaceState.Opened)
{
PowerShell ps = PowerShell.Create();
var getLists = new PSCommand();
getLists.AddCommand("Get-DistributionGroup");
ps.Commands = getLists;
ps.Runspace = runspace;
var response = ps.Invoke();
}
}
}
}
I tried replacing the Basic authentication part with Powershell calls, that should bring up external authentication, but that doesn't seem to work with the C# sdk.
Connect-ExchangeOnline -UserPrincipalName "email@email.com"
this just creates error, and even tried to run Install-Module and Import-Module commands, but in the SDK Install-Module command is unknown and Import doesn't help
The error is:
System.Management.Automation.CommandNotFoundException: 'The term 'Get-DistributionGroup' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.'
Is there any way to get access to Exchange Online commands inside C# or do I need to rewrite it all in PowerShell for it to work.