0

I need your help with my small application.

I need to create easy console application which will sign into SharePoint Online and download some documents from library.

When I am doing this on older SharePoint version it works fine. Unfortunately, its not working on SharePoint Online in Microsoft 365.

string userName = "[email protected]";
            string passwordPlain = "passw0rd";
            string baseUrl = "https://teams.mypage.com/sites/TESTSITE/";

            var passWord = new SecureString();

            foreach (char c in passwordPlain) 
                passWord.AppendChar(c);

            var creds = new SharePointOnlineCredentials(userName, passWord);
            
            try
            {
                var authDiguestToken = GetRequestDigest(creds, baseUrl);
               

                Console.WriteLine("Result " + authDiguestToken);
            }
            catch (Exception ex)
            {
                Console.WriteLine("\n\n Exception: \n\n " + ex.Message);
            }

So in this example I am going to get token value and display on console.

 private static string GetRequestDigest(SharePointOnlineCredentials creds, string baseUrl)
        {
            var client = new RestClient(baseUrl + "_api/");
            var digestRequest = new RestRequest("contextinfo", Method.POST);
            digestRequest.AddHeader("Accept", "application/json");

            //authentication cookie
            client.CookieContainer = new CookieContainer();
            client.CookieContainer.SetCookies(new Uri(baseUrl), creds.GetAuthenticationCookie(new Uri(baseUrl)));

            //I'm using dynamic jsut for demo, it's better to use some strongly typed class  
            return client.Execute<string>(digestRequest).Data;
        }

So in this case I am using cookie, not sure why its not working with other SharePoint Online in Microsoft 365.

Is there something special which needs to be done to get authenticated?

1

4 Answers 4

1

Simple answer is don't use a username and password. Instead, follow the process at Using modern authentication with CSOM for .NET Standard.

Note that this also applies to the .NET Framework; the older version of CSOM.

4
  • Well in previous share point which we used we use vbs script to perform some API calls for that and user name and password was enough. This modern authentication looks nice, need to ask if its possible
    – Odyn
    Commented Jan 6, 2021 at 19:15
  • Was previous on-prem SharePoint? Because that would make sense as in general on-prem farms will use Windows auth. SPO doesn't and with Modern Auth, alternative ways to auth are needed and are more secure.
    – user6024
    Commented Jan 7, 2021 at 0:14
  • Probably yes, it was auth using domain name/user and password. So in SPO I have to use modern authentication way?
    – Odyn
    Commented Jan 7, 2021 at 7:41
  • You should be using Client ID + Certificate (or at worst, Secret), yes.
    – user6024
    Commented Jan 7, 2021 at 15:34
0

SharePointOnlineCredentials still works in my tenant. Some new tenants may disabled LegacyAuthProtocolsEnabled. Could you have a check that setting is enabled?

$TenantSettings = Get-SPOTenant 
$TenantSettings.LegacyAuthProtocolsEnabled
Set-SPOTenant -LegacyAuthProtocolsEnabled $True

More reference:

BR

2
  • I am not a owner of this share point site and also ask them to enable Legacy Authentication Protocols. Not sure if they accept but i want to try.
    – Odyn
    Commented Jan 7, 2021 at 7:39
  • 1
    This is REALLY bad practice and I do not recommend you follow this advice at all. It introduces a major security hole.
    – user6024
    Commented Jan 7, 2021 at 15:34
0

You can try using AuthenticationManager.GetWebLoginClientContext method of OfficeDevPnP.Core Namespace.

I have used this before with CSOM console applications and it works great with SharePoint Online. This method will also work if you have multi-factor authentication enabled on your tenant.

Check this article for more information: Using CSOM To Connect To A SharePoint Site

You can also check other authentication methods at: Authenticate SharePoint Using PnP Authentication Manager

0

Is PnP PowerShell an alternative? You could achieve your requirement with a couple of lines as you have a command to connect and another to download files

https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/connect-pnponline

https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/get-pnpfile?view=sharepoint-ps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.