As you might now, Microsoft recently started promoting Project Server Online. Previously, it was required to host Project Server on your own on-premise systems or in the cloud on Microsoft Azure. Now since both use a slightly different authentication method. I will cover them in this article.

No authentication

If you were to use no authentication with your Project Server installation it would just simply look like this:

var projContext = new ProjectContext(url);

Project Server on-premises installation

var projContext = new ProjectContext(url);
projContext.Credentials = new NetworkCredential(username, password);

Project Server Online

var projContext = new ProjectContext(url);
    
// SecureString is required by constructor of SharePointOnlineCredentials
var secPass = new SecureString();
foreach (var c in password)
{
    secPass.AppendChar(c);
}
projContext.Credentials = new SharePointOnlineCredentials(username, secPass);

Optimization

Because ProjectContext inherits from Microsoft.SharePoint.Client.ClientContext, it also implements IDisposable, which means you should be using it in a ‘using-clause’ as below so resources are released afterwards.

using (var projContext = new ProjectContext(url))
{
   // do your work here
   // additional authentication can go here too
}