Accessing MS Project Server with ProjectContext and ADFS

Cloud Icon

If you need to access Microsoft Project Server, you can use the ProjectContext class from the Microsoft.ProjectServer.Client.dll assembly. This class inherits from the SharePoint ClientContext:


using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.ProjectServer.Client
{
public class ProjectContext : ClientContext
{...

and adds a few new methods and properties.

If your SharePoint is running with ADFS, you can use the GetADFSUserNameMixedAuthenticatedContext-methode to authenticate with the ClientContext. The message needs some additional information like sts (hostname of ADFS), idpId (identifier of the ADFS relying party) … and returns an authenticated ClientContext.


OfficeDevPnP.Core.AuthenticationManager am = new OfficeDevPnP.Core.AuthenticationManager();
ClientContext ctx = am.GetADFSUserNameMixedAuthenticatedContext(
siteUrl: "https://myprojects.mycompany.com",
user: USERNAME,
password: PASSWORD,
domain: "DOMAIN",
sts: "HOSTNAME OF ADFS",
idpId: "sharepoint:myprojects:mycompany");

But the ProjectContext can only be created via the ProjectContext-constructor, the GetADFSUserNameMixedAuthenticatedContext cannot return a ProjectContext and the ClientContext cannot be casted to the ProjectContext. So how to authenticate against ADFS?
The solution is to use GetFedAuthCookie-method from the UsernameMixed()-class. It must be executed in the ExecutingWebRequest-event and applied the federation cookie to the cookie container of the web request:


string projectUrl = "https://myprojects.mycompany.com";
ProjectContext pc = new ProjectContext(projectUrl);
pc.ExecutingWebRequest += delegate (object oSender, WebRequestEventArgs webRequestEventArgs)
{
CookieContainer fedAuth = new OfficeDevPnP.Core.IdentityModel.TokenProviders.ADFS.UsernameMixed().GetFedAuthCookie(
projectUrl,
string.Format("{0}\\{1}", "DOMAIN", USERNAME),
PASSWORD,
new Uri(string.Format("https://{0}/adfs/services/trust/13/usernamemixed", "HOSTNAME OF ADFS")),
"sharepoint:myprojects:mycompany",
10);
webRequestEventArgs.WebRequestExecutor.WebRequest.CookieContainer = fedAuth;
};
var projects = pc.Projects;
pc.Load(projects);
pc.ExecuteQuery();

The article or information provided here represents completely my own personal view & thought. It is recommended to test the content or scripts of the site in the lab, before making use in the production environment & use it completely at your own risk. The articles, scripts, suggestions or tricks published on the site are provided AS-IS with no warranties or guarantees and confers no rights.



Thomas Zühlke
About Thomas Zühlke 3 Articles
Senior Consultant and solution architect for digital transformation and Business solutions in midsize and large companies. SharePoint-fan since SharePoint 2007 and MCPD/MCSD in SharePoint 2010/2013. Now more focued on cloud topics (DevOps, cloud-native-apps, Containers) and especially on Azure.

Be the first to comment

Leave a Reply

Your email address will not be published.


*