3

I am creating Provider Hosted APP and Deploying in Azure and Consuming in my SharePoint 2013 - On Premise.

When I am using:

var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);

var clientContext = spContext.CreateUserClientContextForSPHost()

It's Giving an error: Object reference not set to an instance of an object.

But when I am using:

var contextToken = TokenHelper.GetContextTokenFromRequest(Request);

var hostWeb = Request.QueryString["SPHostUrl"];

var clientContext = TokenHelper.GetClientContextWithContextToken(hostWeb, contextToken, Request.Url.Authority)

It's giving an error: The parameter 'token' cannot be a null or empty string

If I commenting all code in .cs file then APP is working fine. Its showing .aspx file data. Also when I deployed to Office 365 account I am able to take Client Context.

So how can I take ClientContext for on-premise?

I have added below code in web.config:

 <appSettings>
    <add key="ClientId" value="68150c39-ee2d-4d71-aa5d-12d06c52e29a" />
    <add key="ClientSecret" value="C7iwn79rs83sQPeP42iCSO1Oo/9nI6yUbvxJM5j1+G4=" />
  </appSettings>

Also in azure I have mentioned my Client ID and Client Secret:

enter image description here

  • Did you configure your on-premise farm in low-trust scenario? Or are you using high-trust for on-premise? – Sergei Sergeev Dec 29 '16 at 8:37
  • No I have not configured my farm in low-trust scenario. But when I am commenting ".cs" file code APP is working it goes to SharePoint Site context to Azure APP Site. Am i missing any steps to use ClientContext? – Prashant Shah Dec 29 '16 at 8:47
  • What do you have in your web.config? ClientId, ClientSecret? – Sergei Sergeev Dec 29 '16 at 8:52
  • @Kai I have updated my question. – Prashant Shah Dec 29 '16 at 9:05
6

Provider hosted apps are divided into two different categories in terms of authentication mechanism.

These are low-trust apps and high-trust apps, both use OAuth for authentication.

Low trust apps use Azure Access Control Services (ACS) in order to obtain OAuth access token, high trust apps use certificate in order to generate access token right inside the app (that's why high-trust).

For SharePoint online (Office 365) only low-trust app available. Low trust apps use ClientId and CientSecret. For on-premise by default only high-trust availiable, apps use ClientId, ClientSigningCertificatePath, ClientSigningCertificatePassword in web config. Besides it's possible to configure your on-premise farm to work in low trust and use ACS for access tokens. Besides this configuration requires O365 subscription.

Answering your question - it's not possible to easily deploy provider hosted app with high trust authentication into Azure, it will never work. You need either configure low trust or change the code inside TokenHelper.cs to make it work.

Some additional useful reading:

  • Thank you for your answer!!! Can you give me example to deploy app in azure and consume in SP on-premise 2013. I have followed: winwire.com/wp-content/uploads/2015/11/… But I am unable to take ClientContext. Can you help in this scenerio? When I am not using ClientContext working good. – Prashant Shah Dec 29 '16 at 9:38
  • I'm not familiar with document you provided and with connection manager described in the document, so can't help here, sorry. – Sergei Sergeev Dec 29 '16 at 10:11
  • Ok No problem. Kai, do you have any document or article to work with this scenario? Provider Hosted APP hosted in Azure and Deploy on On-Premise SP2013. – Prashant Shah Dec 29 '16 at 11:38
  • I'm not aware of such document, be default TokeHelper.cs and SharePointContext.cs work well with SharePoint Online (low trust) and on-premise in high-trust. When using high trust with TokenHelper.cs you need windows user, becasue of following lines inside WindowsIdentity logonUserIdentity = httpRequest.LogonUserIdentity; The main issue for you is to implement correct authentication in order to obtain ClientContext. For that purpose consider ADFS. With ADFS you can host your provider in the cloud, but have users authenticated with AD domain – Sergei Sergeev Dec 29 '16 at 11:46
  • For ADFS consider following answer and article by Wictor Wilen - here – Sergei Sergeev Dec 29 '16 at 11:48

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.