I created a provider hosted app for SharePoint 2013(OnPrem) which works with the standard "Tokenhelper.cs" to connect to SharePoint.

I use a certificate for authorization which works fine on my app.

Now I also have a TimerJob on the same machine, that needs to communicate with SharePoint. In my TimerJob I do not have any httpContext obviously, which is needed to use the following code:

var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext.Current);
using (var appContext = spContext.CreateAppOnlyClientContextForSPHost()) {}
var web=appContext.Web;

I can connect using credentials instead:

var appContext= new ClientContext(siteUri);
appContext.Credentials = new NetworkCredential(username, password);
var web=appContext.Web;

But I do not want to use any credentials but pure Certificate authentication instead. How can this be done?


You can use below method from TokenHelper:

var SPHostUrl = new Uri("http://your_sp_farm/sites/your_site");
var accessToken = TokenHelper.GetS2SAccessTokenWithWindowsIdentity(SPHostUrl, null);
using(var clientContext = TokenHelper.GetClientContextWithAccessToken(SPHostUrl.AbsoluteUri, accessToken)){...}  

You also need to create app.config for you job (I'm assuming that's console app) and put into the same appSettings as for normal web application (ClientSigningCertificatePath, etc.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.