TokenHelper

solving error signing credentials was not initialized on sharepoint provider apps

Posted on

Today I was trying to create a sharepoint provider hosted app by using many of the blog posts and tutorials on the web.

​​​The code I was using was:


Uri hostWeb = new Uri(Request.QueryString["SPHostUrl"]);
using(var clientContext=TokenHelper.GetS2SClientContextWithWindowsIdentity(hostWeb,Request.LogonUserIdentity))
{
var web= clientContext.Web;
clientContext.Load(web, w=>w.Lists.Include(l=>l.Title).Where(l=> !l.Hidden));
clientContext.ExecuteQuery();
return View(web.Lists);
}

 

The exception was thrown explicitly by the TokenHelper Class

 

21M3r

 

​​sol​​ution​

​Well, if you analyze the code for a second the name of the method is GetS2SClientContextWithWindowsIdentity, but I am creating a Sharepoint Online Provider Hosted App.

Does it make sense to get the client contex from a windows identity? obviously it does not.

After investigating for some minutes, I found there are other methods in the token helper class, and the one below makes more sense, doesnt it?


var contextToken = TokenHelper.GetContextTokenFromRequest(Request);
var hostWeb = Request.QueryString["SPHostUrl"];
using (var clientContext = TokenHelper.GetClientContextWithContextToken(hostWeb, contextToken, Request.Url.Authority))
{
var web = clientContext.Web;
clientContext.Load(web, w=>w.Lists.Include(l=>l.Title).Where(l=> !l.Hidden));
clientContext.ExecuteQuery();
return View(web.Lists);
​}

Advertisements