Home

Connecting to SharePoint with MFA enabled

DevGuy77
New Contributor

I'm trying to create a simple windows 10 desktop app (WPF) that will connect to SharePoint.

 

I have the following code that is taken from an example online, however this account has MFA enabled.

I get the following error:

 

"Microsoft.SharePoint.Client.IdcrlException: The account password has expired, as specified by the account settings.\r\n at Microsoft.SharePoint.Client.Idcrl.IdcrlAuth.GetServiceToken(String securityXml, String serviceTarget, String servicePolicy)

Code as follows:

 

private void button_Click(object sender, RoutedEventArgs e)
{
     var context = GetUserContext();
     var results = context.LoadQuery(context.Web.Lists.Include(list => list.Title, list => list.Id));

     try {
     context.ExecuteQuery();
     }
     catch (Exception err) {
     // Error: Microsoft.SharePoint.Client.IdcrlException: The account password has expired, as specified by the account settings.
     string temp = err.ToString();
     }

     results.ToList().ForEach(x => {
     tbOutput.Text += x.Title;
     });
}

public static ClientContext GetUserContext()
{
     var o365Password = new SecureString();

     foreach(char c in configuration.o365Password) {
     o365Password.AppendChar(c);
     }

     var o365Credentials = new SharePointOnlineCredentials(configuration.o365Email, o365Password);
     var o365Context = new ClientContext(configuration.o365Url);
     o365Context.Credentials = o365Credentials;

     //Web web = o365Context.Web;
     //o365Context.Load(web);
     //o365Context.ExecuteQuery();

     return o365Context;
}

public class configuration
{
     public static string o365Url = "https://example.sharepoint.com/";
     public static string o365Email = "user@example.com";
     public static string o365Password = "password_here";
}
}


How would I fix this?

Thank you

1 Reply
Highlighted

You can't authenticate with a user account if MFA is enabled.

 

Take a look at App-only authentication. There are two methods available for this: via Azure Active Directory or by registering the app directly with SharePoint.

 

https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azuread

 

https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs

Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
38 Replies
Extentions Synchronization
Deleted in Discussions on
3 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
29 Replies
Security Community Webinars
Valon_Kolica in Security, Privacy & Compliance on
12 Replies