none
Error 401 authenticating with Project Online and CSOM RRS feed

  • Question

  • 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 missing?


    ScoutwestInc

    Wednesday, September 2, 2015 8:51 PM

Answers

  • 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.

    • Marked as answer by ScoutwestInc Monday, September 14, 2015 5:48 PM
    Thursday, September 3, 2015 7:32 AM
  • 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. 

    • Marked as answer by ScoutwestInc Monday, September 14, 2015 5:48 PM
    Monday, September 14, 2015 7:22 AM

All replies

  • 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.

    • Marked as answer by ScoutwestInc Monday, September 14, 2015 5:48 PM
    Thursday, September 3, 2015 7:32 AM
  • 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?


    ScoutwestInc

    Thursday, September 10, 2015 6:59 PM
  • 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. 

    • Marked as answer by ScoutwestInc Monday, September 14, 2015 5:48 PM
    Monday, September 14, 2015 7:22 AM