SharePointOnlineCredentials is an object that allows You to authenticate in Office 365(SharePoint 2013 Online) portal. You have to use it when You want to connect to SharePoint online using ClientContext or call any web service hosted within Office 365. The usage of the object is very simple. You just have to create a new instance passing proper arguments and assign it to the ClientContext.Credentials property:

var targetSite = new Uri("<SPOnlinbUrl>");
 var login = "<login>";
 var password = "<password>";
 using (ClientContext ctx = new ClientContext(targetSite))
 {
 var securePassword = new SecureString();
 foreach (char c in password)
 {
 securePassword.AppendChar(c);
 }

ctx.Credentials = new SharePointOnlineCredentials(login, securePassword);

// ....
 }

Sometimes, during creation of SharePointOnlineCredentials, System.FileNotFound exception occurs and it says that msoidcliL.dll file is missing.

First thing You have to check in this situation is if the msoidcliL.dll exists in the directory C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\Client. If either directory or the dll file does not exist, then download and install SharePoint Server 2013 Client Components SDK. Now, check again if the above code works. If yes,  then You are ready to connect to SharePoint Online. If not(what happened in my case), You have to check one more thing – the registry entry.

At the beginning, when I got this error I was confused, because I didn’t know where to search for the root cause. After some time I started to investigate the error step by step, which means to use iLSpy and decompile the Microsoft.SharePoint.Client.Runtime.dll. While browsing the assembly I found an interesting part:

CsomDir

I opened the registry and checked the path. It did not exist, so I created the missing path in the registry and at the end added a new entry CsomDir with the value C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\Client.

CsomReg

After restarting the machine the code started to work. It seems that this missing registry entry was causing all the problems. Now I can connect to Office 365 without any issues.