How to traverse SharePoint site collection with Multi Factor Authentication (MFA) enabled?

%3CLINGO-SUB%20id%3D%22lingo-sub-1270067%22%20slang%3D%22en-US%22%3EHow%20to%20traverse%20SharePoint%20site%20collection%20with%20Multi%20Factor%20Authentication%20(MFA)%20enabled%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1270067%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20a%20Winforms%20app%20that%20displays%20a%20SharePoint%20site%20collection%20and%20document%20libraries%20in%20a%26nbsp%3BTreeView%26nbsp%3Bcontrol%20with%20hierarchy%20according%20to%20the%20site%20level.%3C%2FP%3E%3CP%3EI%20use%20the%20following%20code%20for%20account%20with%20MFA%20enabled%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3EAuthenticationManager%20authManager%20%3D%20new%20AuthenticationManager()%3B%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20using%20(ClientContext%20clientContext%20%3D%20authManager.GetWebLoginClientContext(siteUrl))%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Web%20web%20%3D%20clientContext.Web%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20WebCollection%20site%20%3D%20web.GetSubwebsForCurrentUser(null)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20clientContext.Load(site%2C%20we%20%3D%26gt%3B%20we.Include(w%20%3D%26gt%3B%20w.Url%2C%20w%20%3D%26gt%3B%20w.Title))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20clientContext.ExecuteQuery()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20teamSites%20%3D%20site.ToDictionary(w%20%3D%26gt%3B%20w.Url%2C%20w%20%3D%26gt%3B%20w.Title)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20teamSites%20%3D%20teamSites.OrderBy(kvp%20%3D%26gt%3B%20kvp.Value).ToDictionary(k%20%3D%26gt%3B%20k.Key%2C%20k%20%3D%26gt%3B%20k.Value)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ListCollection%20libraries%20%3D%20web.Lists%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20clientContext.Load(libraries%2C%20l%20%3D%26gt%3B%20l.Include(li%20%3D%26gt%3B%20li.DefaultViewUrl%2C%20li%20%3D%26gt%3B%20li.BaseType%2C%20li%20%3D%26gt%3B%20li.Title%2C%20li%20%3D%26gt%3B%20li.BaseTemplate%2C%20li%20%3D%26gt%3B%20li.Hidden))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20clientContext.ExecuteQuery()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20documentLibraries%20%3D%20libraries.Where(lib%20%3D%26gt%3B%20lib.BaseType%20%3D%3D%20BaseType.DocumentLibrary%20%26amp%3B%26amp%3B%20lib.Hidden%20%3D%3D%20false%20%26amp%3B%26amp%3B%20lib.BaseTemplate%20%3D%3D%20101).ToList()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EThe%20team%20site%20URL%20will%20be%20added%20to%20each%26nbsp%3BTreeNode%26nbsp%3Bin%20my%26nbsp%3BTreeView%2C%20whenever%20user%20clicks%20on%20a%26nbsp%3BTreeNode%2C%20it%20will%20need%20to%20load%20the%20sub-sites%20and%20document%20libraries%20using%20the%20above%20method.%20However%2C%20every%20time%20it%20loads%2C%20the%20pop%20up%20shows%20and%20disappears%20because%20user%20is%20already%20logged%20in.%20Is%20there%20anyway%20to%20prevent%20it%20popping%20up%20every%20single%20time%20user%20clicks%20on%20the%20site%26nbsp%3BTreeNode%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1272471%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20traverse%20SharePoint%20site%20collection%20with%20Multi%20Factor%20Authentication%20(MFA)%20enabled%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1272471%22%20slang%3D%22en-US%22%3EWhat%20about%20not%20using%20user%20and%20password%20to%20connect%20to%20SPO%3F%20You%20could%20connect%20as%20an%20App%20so%20you%20could%20avoid%20MFA%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1273059%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20traverse%20SharePoint%20site%20collection%20with%20Multi%20Factor%20Authentication%20(MFA)%20enabled%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1273059%22%20slang%3D%22en-US%22%3EThis%20program%20is%20for%20end-users%20to%20connect%20to%20their%20SP%20environments.%20Can%20%22Connecting%20as%20an%20App%22%20work%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1273725%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20traverse%20SharePoint%20site%20collection%20with%20Multi%20Factor%20Authentication%20(MFA)%20enabled%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1273725%22%20slang%3D%22en-US%22%3EThat's%20correct%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1273771%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20traverse%20SharePoint%20site%20collection%20with%20Multi%20Factor%20Authentication%20(MFA)%20enabled%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1273771%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F60%22%20target%3D%22_blank%22%3E%40Juan%20Carlos%20Gonz%C3%A1lez%20Mart%C3%ADn%3C%2FA%3E%26nbsp%3B%3CBR%20%2F%3EI%20followed%20this%20article%20to%20set%20things%20up.%26nbsp%3B%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%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsharepoint%2Fdev%2Fsolution-guidance%2Fsecurity-apponly-azureacs%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBut%20I%20keep%20getting%20the%20error%20(see%20attachment).%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1277047%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20traverse%20SharePoint%20site%20collection%20with%20Multi%20Factor%20Authentication%20(MFA)%20enabled%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1277047%22%20slang%3D%22en-US%22%3EBtw%2C%20the%20end-users%20will%20connect%20to%20their%20OWN%20SP%20environments%2C%20not%20ours.%20So%20with%20the%20app-only%20flow%2C%20I%20don't%20think%20it%20will%20work%20for%20others'%20SP%20environments%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor

I have a Winforms app that displays a SharePoint site collection and document libraries in a TreeView control with hierarchy according to the site level.

I use the following code for account with MFA enabled

 

 

AuthenticationManager authManager = new AuthenticationManager();            
            using (ClientContext clientContext = authManager.GetWebLoginClientContext(siteUrl))
            {
                Web web = clientContext.Web;
                WebCollection site = web.GetSubwebsForCurrentUser(null);
                clientContext.Load(site, we => we.Include(w => w.Url, w => w.Title));
                clientContext.ExecuteQuery();
                teamSites = site.ToDictionary(w => w.Url, w => w.Title);
                teamSites = teamSites.OrderBy(kvp => kvp.Value).ToDictionary(k => k.Key, k => k.Value);
                ListCollection libraries = web.Lists;
                clientContext.Load(libraries, l => l.Include(li => li.DefaultViewUrl, li => li.BaseType, li => li.Title, li => li.BaseTemplate, li => li.Hidden));
                clientContext.ExecuteQuery();
                documentLibraries = libraries.Where(lib => lib.BaseType == BaseType.DocumentLibrary && lib.Hidden == false && lib.BaseTemplate == 101).ToList();
            }

 

 

The team site URL will be added to each TreeNode in my TreeView, whenever user clicks on a TreeNode, it will need to load the sub-sites and document libraries using the above method. However, every time it loads, the pop up shows and disappears because user is already logged in. Is there anyway to prevent it popping up every single time user clicks on the site TreeNode?

5 Replies
Highlighted
What about not using user and password to connect to SPO? You could connect as an App so you could avoid MFA
Highlighted
This program is for end-users to connect to their SP environments. Can "Connecting as an App" work?
Highlighted
Highlighted
Highlighted
Btw, the end-users will connect to their OWN SP environments, not ours. So with the app-only flow, I don't think it will work for others' SP environments