Forum Discussion

DevGuy77's avatar
DevGuy77
Copper Contributor
Jul 23, 2019

Connecting to SharePoint with MFA enabled

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://cpcabcouk.sharepoint.com/";
     public static string o365Email = "user@example.com";
     public static string o365Password = "password_here";
}
}


How would I fix this?

Thank you

1 Reply

Resources