Login problems when using OfficeDevPnP.Core „GetWebLoginClientContext“.
I have been struggling with the login to Sharepoint using the web based login. The problem is also described here and here.
var authManager = new OfficeDevPnP.Core.AuthenticationManager(); var context = authManager.GetWebLoginClientContext(siteUrl);
When the „GetWebClientContext“ is called, the framework opens a small login windows. The window hosts a Webbrowser control, which opens the Sharepoint site. If you are not already logged in, a login is shown. This also works in 2-factor authentication.
In the background, the whole thing is used to read the login cookies from the browser and construct a ClientContext with the cookies. I came across two problems. One is that the login is not recognized and the Sharepoint site opens in the login window. The other is a white login windows, which does not load. In both cases the login window is stuck and has to be closed manually. This results in a failure contructing the ClientContext.
Login window opens the Sharepoint site
In some cases the Sharepoint site opens in the login window. The problem here is, that the wrong cookie is read from the Webbrowser control. The library and the „AuthenticationManager“ class is looking for two cookies: FedAuth or EdgeAccessCookie.
In most cases the FedAuth cookies was there, but not always.
In some cases the cookie was missing, but there was another cookie, named „SPOIDCRL“. I found a hint on Stackoverflow, that this might also be a valid cookie for the login.
Since the library is end of life, I did a fork of the „PnP Sites Core Library“ and added the „SPOIDCRL“ cookie to the list of potential auth cookies.
And it worked! The cookie can also be used as valid authentication cookie for the „ClientContext“.
White login window
The second problem was a white login window. In these cases the WebbrowserControl would not load the Sharepoint site. In such a case, the „Navigated“ event in the code is never called, resulting in a stalled login window.
The workaround for that was to implement a reload button, the button stops the browser and reloads the page. I also added a timer which does an automatic reload after 8 seconds.
The timer is deactivated in the „Nagivated“ event, since if a login is needed, the page should not reload. This solved the problem with the stalled window.
Conclusion
For now it seems to be working, and the problem is solved. The library is end of live and users are recommended to move to the new library „PnP Framework„. However, this library does not offer a simple login solution like the „GetWebLoginClientContext“.
Links
Links to my changes: