I am trying to input SMS code into the below script & that is failing .
# Source : https://www.entraneer.com/blog/entra/authentication/transactional-mfa-entra-id
$secret = ""
$email = ""
$tenantId = ""
$clientId = "" # this is the same for everyone
Write-Host "Get MFA Client Access Token" -ForegroundColor Cyan
$body = @{
'resource' = 'https://adnotifications.windowsazure.com/StrongAuthenticationService.svc/Connector'
'client_id' = $clientId
'client_secret' = $secret
'grant_type' = "client_credentials"
'scope' = "openid"
}
$mfaClientToken = Invoke-RestMethod -Method post -Uri "https://login.microsoftonline.com/$tenantId/oauth2/token" -Body $body
Write-Host "Done." -ForegroundColor Green
Write-Host "Send MFA challenge to the user" -ForegroundColor Green
$XML = @"
<BeginTwoWayAuthenticationRequest>
<Version>1.0</Version>
<UserPrincipalName>$email</UserPrincipalName>
<Lcid>en-us</Lcid>
<ContextId>bb07a24c-e5dc-4983-afe7-a0fcdc049cf7</ContextId>
<SyncCall>true</SyncCall>
<RequireUserMatch>true</RequireUserMatch>
<CallerName>radius</CallerName>
<CallerIP>UNKNOWN:</CallerIP>
<PreferredAuthenticationMethod>TextMessage</PreferredAuthenticationMethod>
</BeginTwoWayAuthenticationRequest>
"@
$headers = @{ "Authorization" = "Bearer $($mfaClientToken.access_token)" }
$mfaResult = Invoke-RestMethod -Uri 'https://strongauthenticationservice.auth.microsoft.com/StrongAuthenticationService.svc/Connector///BeginTwoWayAuthentication' -Method POST -Headers $Headers -Body $XML -ContentType 'application/xml'
Write-Host "Done."
$mfaChallengeMessage = $mfaResult.BeginTwoWayAuthenticationResponse.Result.Message
Write-Host $mfaChallengeMessage
if ($mfaResult.BeginTwoWayAuthenticationResponse.Result.Value -eq "Success") {
Write-Host "OTP sent to your phone. Please enter the OTP:" -ForegroundColor Cyan
$otpCode = Read-Host "Enter the OTP sent via SMS"
$XML = @"
<ValidatePinRequest>
<Version>1.0</Version>
<ContextId>$($mfaResult.BeginTwoWayAuthenticationResponse.ContextId)</ContextId>
<Pin>$otpCode</Pin>
</ValidatePinRequest>
"@
$mfaValidationResult = Invoke-RestMethod -Uri 'https://strongauthenticationservice.auth.microsoft.com/StrongAuthenticationService.svc/Connector//ValidatePin' -Method POST -Headers $Headers -Body $XML -ContentType 'application/xml'
if ($mfaValidationResult.ValidatePinResponse.Result.Value -eq "Success") {
Write-Host "User: `"$email`" successfully validated SMS OTP" -ForegroundColor Green
}
else {
Write-Warning "Invalid OTP or validation failed"
}
}
else {
Write-Warning "MFA Request failed: $($mfaResult.BeginTwoWayAuthenticationResponse.Result.Message)"
}
Error
Get MFA Client Access Token
Done.
Send MFA challenge to the user
Done.
OTP sent to your phone. Please enter the OTP:
Enter the OTP sent via SMS: 696632
Invoke-RestMethod: C:\Git_Repo\MFA_Test\MFATestWIthKyle\sms.ps1:54:28
Line |
54 | … ionResult = Invoke-RestMethod -Uri 'https://strongauthenticationservi …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Service BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px;
| } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold;
| text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color:
| #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px
| solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom:
| 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family:
| Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word;
| } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid;
| border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid;
| border-bottom: 2px white solid; background-color: #e5e5cc;} Service Endpoint not found.
WARNING: Invalid OTP or validation failed
Below line causing the issue.
$mfaValidationResult = Invoke-RestMethod -Uri ‘https://strongauthenticationservice.auth.microsoft.com/StrongAuthenticationService.svc/Connector//ValidatePin’ -Method POST -Headers $Headers -Body $XML -ContentType ‘application/xml’