Connecting to remote server outlook.office365.com failed with the following error message : Access is denied.

  • Thread starter Thread starter Allan. V. Raven
  • Start date Start date
A

Allan. V. Raven

Guest
Hello Team.

Please same my sanity!

Bellow is a simplified version of my code. It was working without any issues 2 weeks ago.

When i run the code i get the error: Connecting to remote server outlook.office365.com failed with the following error message : Access is denied.

This is what i know. I have correct username password and no MFA issue. This account does not require MFA. If i replace Connect-ExchangeOnline with Connect-AzureAD or Connect-MSOL it works. which indicated that username\password are correct.

This code works on the same computer in another project. If i copy PowerShell command to PowerShell it will run without any issue.

Summary: This code does not work only for ExchangOnline in 1 project. I would appreciate any ideas on what may cause this.
I have a workarounds in place, but i need to fix for this project.

protected void cgetLAPSP_click(object sender, EventArgs e)
{
string psscript = @"
[string]$userName = 'myuser@mydomain.onmicrosoft.com'
[string]$userPassword = 'Mypassword'

[securestring]$secStringPassword = ConvertTo-SecureString $userPassword -AsPlainText -Force
[pscredential]$credOject = New-Object System.Management.Automation.PSCredential ($userName, $secStringPassword)

Connect-ExchangeOnline -Credential $credOject
Disconnect-ExchangeOnline -Confirm:$false
";
PowerShell ps = PowerShell.Create();
ps.AddScript(psscript);
Collection<PSObject> psresult = ps.Invoke();
}

Continue reading...
 
Back
Top