How to create a Provider Hosted Application on Form Based Authentication WebApplication in SharePoint 2013

Sathish Nadarajan
Solution Architect
June 8, 2015
Rate this article
[Total: 0    Average: 0/5]

In these previous articles, PHA and FBA, we saw how to create a Provider Hosted Application and How to make a Web Application as Form Based Authentication in SharePoint 2013. In this article, now, let us see how to integrate both of them. i.e., A Provider Hosted App which retrieves the Client Context of a SharePoint Web Application which is a Form Based Authentication using Visual Studio 2013.

One important thing we need to identify here is, there is a new class called SharePointContext.cs has been introduced by Microsoft with Visual Studio 2013, which is not there in Visual Studio 2012. Please find the reference here.

With that information, the rest of the steps are same as that of an ordinary Provider Hosted Application. Refer here

Everytime, I discuss about PHA, I am considering only the High Trust Apps. We had seen enough information about those in the past articles. Hence, I am targeting only, how to convert our AppWeb into Form Based Authentication, so that it can fetch the Client Context without any issues. Once, we fetch the client context, then the remaining things will be very smooth I guess.

To convert the Appweb as Form Based Authentication, there is nothing much. Only modifying the AppWeb’s web.config will do the magic. The sample web.config file is as follows. I request the readers to modify their web.config with appropriate values from their environment.

The sample web.config will be as below.

 <?xml version="1.0" encoding="utf-8"?>
   For more information on how to configure your ASP.NET application, please visit
     <compilation debug="true" targetFramework="4.5" />
     <httpRuntime targetFramework="4.5" />
       <deny users="?" />
         <authentication mode="Forms" />
     <add key="ClientId" value="7c6413ef-3c84-4f04-8912-b7b2c7e43ef9" />
     <add key="ClientSigningCertificatePath" value="C:\SATHISH\Certificates\PHACertificate.pfx" />
     <add key="ClientSigningCertificatePassword" value="Password11" />
     <add key="IssuerId" value="928a1244-8b2e-4d93-9e09-5c72445d15ca" />
         <!--Used by app for SharePoint-->
         <binding name="secureBinding">
           <security mode="Transport" />
       <add binding="basicHttpBinding" scheme="https" bindingConfiguration="secureBinding" />
         <directoryBrowse enabled="true" />

Then on the Default.aspx.cs, let us try to get the ClientContext.

 protected void Page_Load(object sender, EventArgs e)
             // The following code gets the client context and Title property by using TokenHelper.
             // To access other properties, the app may need to request permissions on the host web.
             var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);
             using (var clientContext = spContext.CreateUserClientContextForSPHost())

We can see a difference here. That is till Visual Studio 2012 Era, we used the TokenHelper.cs directly to get the ClientContext. But now in Visual Studio 2013, we can get the ClientContext by means of a new Class called SharePointContext.CS. I have mentioned a link earlier in this article, which describes about the SharePointContext.cs in detail.

Hope this is very easy right..

Download the Sample Config Files Here

Happy Coding,

Sathish Nadarajan.

Author Info

Sathish Nadarajan
Solution Architect
Rate this article
[Total: 0    Average: 0/5]
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 13+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business more

Leave a comment