Error 401 authenticating with Project Online and CSOM

I get error 401 (or 403) when trying to connect to Project Online with CSOM in a console app. (This is not on-premise. It is Microsoft Project Online 2013.) Here is the code.

ProjectContext projContext = new ProjectContext(pwaPath);
projContext.Credentials = new NetworkCredential("myUserID", "mypwd", "xxx.onmicrosoft.com");
projContext.ExecutingWebRequest += new EventHandler<WebRequestEventArgs>(projContext_ExecutingWebRequest);
projContext.Load(projContext.Projects);
projContext.ExecuteQuery();
// Error 401 Unauthorized

static void projContext_ExecutingWebRequest(object sender, WebRequestEventArgs e)
{
    e.WebRequestExecutor.WebRequest.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
}

And another try, without ExecutingWebRequest:

ProjectContext projContext = new ProjectContext(pwaPath);
projContext.Credentials = new NetworkCredential("myUserID", "mypwd", "xxx.onmicrosoft.com");
projContext.Load(projContext.Projects);
projContext.ExecuteQuery();
// Error 403 Forbidden

Q1: Are there any problems with the code?

Q2: Is there a setting in Project Online that I'm mi

September 2nd, 2015 4:55pm

The authentication doesn't work the way you try to do it. You need to make use of the SharePointOnlineCredentials class.

var projectContext = new ProjectContext(pwaPath);
var securePassword = new SecureString();
foreach (var character in "mypwd")
     securePassword.AppendChar(character);

projectContext.Credentials = new SharePointOnlineCredentials("myuser@xxx.onmicrosoft.com", securePassword);

The SharePointOnlineCredentials class is located in the Microsoft.SharePoint.Client.Runtime library.

I hope this helps you to get your application running.

Free Windows Admin Tool Kit Click here and download it now
September 3rd, 2015 3:36am

Excellent!  This works for Project Online.  But it does not work on my customer's on-premise Project Server installation.  They still get '401 Unauthorized'.

Q1: Is there any way to troubleshoot error 401 in an on-premise situation?  Error log, etc?

Q2: Is there some special configuration needed for on-premise installations?

September 10th, 2015 6:59pm

Yepp, for on premise you normally have windows auth in use. 

You can use:

ProjectContext projContext = new ProjectContext(pwaPath);
projContext.Credentials = new NetworkCredential("myUserID", "mypwd", "your domain");

or if your user who is running the program has the appropriate rights granted

ProjectContext projContext = new ProjectContext(pwaPath);
projContext.Credentials = CredentialCache.DefaultCredentials;

I hope this helps you.

PS: I don't know if there is any special configuration needed for the on premise installation. 

Free Windows Admin Tool Kit Click here and download it now
September 14th, 2015 3:25am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics