Home

Connecting to SharePoint with MFA enabled

%3CLINGO-SUB%20id%3D%22lingo-sub-769269%22%20slang%3D%22en-US%22%3EConnecting%20to%20SharePoint%20with%20MFA%20enabled%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-769269%22%20slang%3D%22en-US%22%3E%3CP%3EI'm%20trying%20to%20create%20a%20simple%20windows%2010%20desktop%20app%20(WPF)%20that%20will%20connect%20to%20SharePoint.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20the%20following%20code%20that%20is%20taken%20from%20an%20example%20online%2C%20however%20this%20account%20has%20MFA%20enabled.%3CBR%20%2F%3E%3CBR%20%2F%3EI%20get%20the%20following%20error%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CBLOCKQUOTE%3E%22Microsoft.SharePoint.Client.IdcrlException%3A%20The%20account%20password%20has%20expired%2C%20as%20specified%20by%20the%20account%20settings.%5Cr%5Cn%20at%20Microsoft.SharePoint.Client.Idcrl.IdcrlAuth.GetServiceToken(String%20securityXml%2C%20String%20serviceTarget%2C%20String%20servicePolicy)%3C%2FBLOCKQUOTE%3E%3CP%3ECode%20as%20follows%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eprivate%20void%20button_Click(object%20sender%2C%20RoutedEventArgs%20e)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bvar%20context%20%3D%20GetUserContext()%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bvar%20results%20%3D%20context.LoadQuery(context.Web.Lists.Include(list%20%3D%26gt%3B%20list.Title%2C%20list%20%3D%26gt%3B%20list.Id))%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Btry%20%7B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bcontext.ExecuteQuery()%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%7D%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bcatch%20(Exception%20err)%20%7B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%2F%2F%20Error%3A%20Microsoft.SharePoint.Client.IdcrlException%3A%20The%20account%20password%20has%20expired%2C%20as%20specified%20by%20the%20account%20settings.%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bstring%20temp%20%3D%20err.ToString()%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%7D%3CBR%20%2F%3E%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bresults.ToList().ForEach(x%20%3D%26gt%3B%20%7B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BtbOutput.Text%20%2B%3D%20x.Title%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%7D)%3B%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3Epublic%20static%20ClientContext%20GetUserContext()%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bvar%20o365Password%20%3D%20new%20SecureString()%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bforeach(char%20c%20in%20configuration.o365Password)%20%7B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bo365Password.AppendChar(c)%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bvar%20o365Credentials%20%3D%20new%20SharePointOnlineCredentials(configuration.o365Email%2C%20o365Password)%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bvar%20o365Context%20%3D%20new%20ClientContext(configuration.o365Url)%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bo365Context.Credentials%20%3D%20o365Credentials%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%2F%2FWeb%20web%20%3D%20o365Context.Web%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%2F%2Fo365Context.Load(web)%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%2F%2Fo365Context.ExecuteQuery()%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Breturn%20o365Context%3B%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3Epublic%20class%20configuration%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bpublic%20static%20string%20o365Url%20%3D%20%22%3CA%20href%3D%22https%3A%2F%2Fcpcabcouk.sharepoint.com%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fexample.sharepoint.com%2F%3C%2FA%3E%22%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bpublic%20static%20string%20o365Email%20%3D%20%22user%40example.com%22%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bpublic%20static%20string%20o365Password%20%3D%20%22password_here%22%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EHow%20would%20I%20fix%20this%3F%3CBR%20%2F%3E%3CBR%20%2F%3EThank%20you%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-806263%22%20slang%3D%22en-US%22%3ERe%3A%20Connecting%20to%20SharePoint%20with%20MFA%20enabled%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-806263%22%20slang%3D%22en-US%22%3E%3CP%3EYou%20can't%20authenticate%20with%20a%20user%20account%20if%20MFA%20is%20enabled.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETake%20a%20look%20at%20App-only%20authentication.%20There%20are%20two%20methods%20available%20for%20this%3A%20via%20Azure%20Active%20Directory%20or%20by%20registering%20the%20app%20directly%20with%20SharePoint.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsharepoint%2Fdev%2Fsolution-guidance%2Fsecurity-apponly-azuread%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsharepoint%2Fdev%2Fsolution-guidance%2Fsecurity-apponly-azuread%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsharepoint%2Fdev%2Fsolution-guidance%2Fsecurity-apponly-azureacs%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsharepoint%2Fdev%2Fsolution-guidance%2Fsecurity-apponly-azureacs%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
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

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
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
Tabs and Dark Mode
cjc2112 in Discussions on
30 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