Share via


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

Question

Monday, July 6, 2020 7:30 PM

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 = '[email protected]'
        [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();
}

All replies (1)

Tuesday, July 7, 2020 6:31 AM

Hi Allan,

Thank you for posting here.

Since this code worked well before, and now it can also be used in another project on the same computer, then I think this code itself may not be wrong.

See if the content in these two documents is helpful to you.

Connect to Exchange Online PowerShell using multi-factor authentication

"Access is denied" error when you connect to Exchange Online by using remote Windows PowerShell

Best Regards,

Timon

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].