4

I am using CSOM library with C# to upload some files to SharePoint. One requirement is to use 2FA. Basically, I am following this post: https://knowledge-junction.com/2017/12/24/office-365-connecting-to-sharepoint-online-site-using-csom-when-multi-factor-authentication-mfa-is-enabled-for-the-user/

The login is done with the help of SharePointPnPCoreOnline library. This is working perfectly.

When the user selects "remember me" during the login procedure, the user stays logged in even when the app is restarted. Now, I am wondering how can I manually log out? I do not see any method or hint how to do that.

caldicot
  • 195
  • 2
  • 15

3 Answers3

0

You can take a look at the code which is used to sign-in https://github.com/SharePoint/PnP-Sites-Core/blob/master/Core/OfficeDevPnP.Core/AuthenticationManager.cs

and per analogiam do the sign-out with the URL "https://login.microsoftonline.com/common/oauth2/logout".

Ciamas
  • 41
  • 2
  • 7
  • I already looked through that code. As far as I understand, the PnP Library just gets the cookies and put them in a `CookieContainer`. So far so good. I had the same idea and tried calling a logout url. Maybe it was the wrong one. I tried calling `https://myid.sharepoint.com/TeamSite/_layouts/15/SignOut.aspx` with a `WebRequest`. But I think I was in the wrong context or I made a mistake. It was not working. Could you give me a small code snippet showing how to call the logout url within the `ClientContext`? – caldicot Mar 14 '19 at 16:22
  • I tried calling the mentioned logout URL, but with no success. I think I am calling the URL within the wrong context. – caldicot Mar 15 '19 at 10:19
0

AuthenticationManager uses IE browser to perform web login and saves auth cookies in CookieContainer. So you need to delete them. Open IE->Settings->InternetOptions->Browsing history->Delete Select Cookies and website data checkbox-> Delete

0

I was searching all over the Internet, tried deleting FedAuth and rtFa cookies, all kinds of logout URLs just like what @Ciamas mentioned, and the way of removing cookies from Internet Properties in Control Panel (It just let me pick another account but my credentials still saved). So, in conclusion, we just can't if we are using AuthenticationManager. In my case is

authManager.GetWebLoginClientContext(siteUrl);

The workaround solution is to use other methods like below and wait for the token to expire.

authManager.GetAppOnlyAuthenticatedContext(siteUrl, clientId, clientSecret);

To get the clientId and clientSecret, just follow these steps.

Jason Tan
  • 56
  • 1
  • 8