Microsoft 365 : PowerShell – Connect-PnPOnline – resolving error – AADSTS50126: Error validating credentials due to invalid username or passwordMicrosoft – even though user name and password are correct

Hi All,
Greetings for the day!!! Today new issue and solution !!!
Use Case / Background
- We need to connect one of our SharePoint site in our tenant
- Though we can use Connect-SPOService and Get-SPOSite but we are using PnP CMDLETs since for further operations its easy with PnP Online
- So we are using Connect-PnPOnline CMDLET to connect to our SiteCollections
- We are connecting with our UserName and Password
- While connecting to our SharePoint site with Connect-PnPOnline CMDLET with correct user name and password but then we are getting an error – error validating credentials
Connect-PnPOnline -Url https://knowledgejunction1.sharepoint.com/demoremoveuserpermissions
- The above CMDLET prompt for user name and password and we were sure that we entered correct user name and password
Issue / Error :
Connect-PnPOnline : AADSTS50126: Error validating credentials due to invalid username or password.
Trace ID: b49df603-9ccc-4d49-be1e-803b51431501
Correlation ID: d714ca32-14c9-4a90-9c4f-51a2e4312ebb
Timestamp: 2023-04-02 23:59:51Z
At line:1 char:1
- Connect-PnPOnline
~~~~~- CategoryInfo : NotSpecified: (:) [Connect-PnPOnline], MsalUiRequiredException
- FullyQualifiedErrorId : Microsoft.Identity.Client.MsalUiRequiredException,PnP.PowerShell.Commands.Base.ConnectOnline

Cause / Why such issue
- This is the error because we are connecting the site with user which has “multi-factor authentication” enabled
- So when we have multi-factor authentication is in place for users and we try to do normal connection we get an issue / error
Solution
- To avoid this error we can connect with either of following two parameters
- UseWebLogin
- With this parameter we connect to SharePoint site using cookie based legacy authentication
- With this type authentication we are limited to few functionality
- We can not use GRAPH APIs with this type of authentication
- Also few functionality related to provisioning templates like – Get-PnPSiteTemplate, Get-PnPTenantTemplate, Invoke-PnPSiteTemplate, Invoke-PnPTenantTemplate can not be used
- Interactive
- With this parameter we connect to SharePoint site and can use GRPAH APIs as well
- Basically it connects to Azure AD and acquires access token
- UseWebLogin
Connect-PnPOnline -UseWebLogin -Url
Connect-PnPOnline -Interactive -Url
We have very good articles on Connect-PnPOnline, have a look once
- Microsoft 365 – Few approaches / options for Connecting to tenant using PnP PowerShell – Connect-PnPOnline – Part 1 – https://knowledge-junction.in/2021/12/16/microsoft-365-few-approaches-options-for-connecting-to-tenant-using-pnp-powershell-connect-pnponline-part-1/
- Microsoft 365 – Connecting to tenant using PnP PowerShell – Connect-PnPOnline – exploring DeviceLogIn (Device code flow) parameter- Part 2 – https://knowledge-junction.in/2021/12/18/microsoft-365-connecting-to-tenant-using-pnp-powershell-connect-pnponline-exploring-devicelogin-device-code-flow-parameter-part-2/
References
- Connect-PnPOnline – Connect-PnPOnline | PnP PowerShell
2 Responses
[…] We have one more another article – connecting Microsoft 365 tenant using PnP PowerShell when MFA is enabled – Microsoft 365 : PowerShell – Connect-PnPOnline – resolving error – AADSTS50126: Error validating credentials due to invalid username or password Microsoft – even though user name and password are correct – https://knowledge-junction.in/2023/04/03/microsoft-365-powershell-connect-pnponline-resolving-error-… […]
[…] Microsoft 365 : PowerShell – Connect-PnPOnline – resolving error – AADSTS50126: Error validating credentials due to invalid username or password – even though user name and password are correct – https://knowledge-junction.in/2023/04/03/microsoft-365-powershell-connect-pnponline-resolving-error-… […]
You must log in to post a comment.