[SOLVED] Authentication Error while connecting to Msolservice using azure functions

Question

This Content is from Stack Overflow. Question asked by Ananda Bhavani Gedela

I have deployed a function app which includes an azure function that connects to msolservice. But this is producing a Authentication Error recently.
script I am using:

$username = 'xxx' 
$password = 'xxx' 
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $username, $(convertto-securestring $password -asplaintext -force)
Connect-MsolService -Credential $cred

Error it produced:
Connect-MsolService : Authentication Error: Unexpected authentication failure.

Function App details
Function runtime: Powershell
runtime version: 3.8.2.0

The user is not MFA enabled.
And I am able to connect it from my local powershell. Any help regarding this issue can be helpful

Solution

I had the same issue come up this week. I fixed it by doing three things:

  1. Adding the following line of code to the script before the first connection to force TLS 1.2
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    
  2. Uninstalling the Azure AD Powershell module from the Windows control panel. This module is obsolete.
  3. Installing the new MSOnline Powershell module with the following command in a Powershell prompt with administrator permissions:
    Install-Module -Name MSOnline -Force
    

I think the older module must have been using an API or service endpoint that was retired somewhere in the last couple weeks.


Separately, it’s worth mentioning here the two other common issues I found while troubleshooting this. First, the service account used to make the connection must be a full Global Administrator. Second, if this script is to be fully automated the account cannot use MFA. This is going to cause me more problems soon, as we’re working on requiring MFA pretty much everywhere.

Answered by Joel Coehoorn


This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 4.0.

people found this article helpful. What about you?