{"id":8675,"date":"2016-01-14T21:48:50","date_gmt":"2016-01-14T21:48:50","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/making-sharepoint-app-access-token-caching-work-in-mvc\/"},"modified":"2016-07-28T10:57:31","modified_gmt":"2016-07-28T10:57:31","slug":"making-sharepoint-app-access-token-caching-work-in-mvc","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/making-sharepoint-app-access-token-caching-work-in-mvc\/","title":{"rendered":"Making SharePoint App Access Token Caching work in MVC"},"content":{"rendered":"<p>I&#8217;ve put together a couple of SharePoint Apps that use MVC to run the remote web application.&#160; When you use MVC for a SharePoint remote web, Visual Studio automatically includes some helpful files to help manage the SharePoint access token.&#160; I decided to go ahead and try to use these files to help me out, but I ran into an issue&#8230; they don&#8217;t work.&#160; At least not right off. <\/p>\n<p>After digging around for a bit, I noticed that the access caching mechanism was working fine.&#160; But there is a JavaScript file named <strong>spcontext.js<\/strong> that you have to include that automatically rewrites URLs to append the SPHostUrl value that the caching mechanism uses to retrieve the access token. I included the file, but none of my URLs were being rewritten.&#160; The culprit appears to be the following line of code:<\/p>\n<p> <code><strong>if (queryString[0] === \"?\") { <\/strong>    <br \/>&#160;&#160;&#160;&#160; queryString = queryString.substring(1);     <br \/>} <\/code>  <\/p>\n<p>This is checking to see if the first character of the query string is a ? and if so, it strips it off.&#160; The problem is that queryString[0] always returns null and does not return the first character of the string.&#160; This left the ? character in place which messed up the query string parser.&#160; I went in and updated the code to use the <strong>charAt<\/strong> function instead:<\/p>\n<p> <code><strong>if (queryString.charAt(0) === \"?\") { <\/strong>    <br \/>&#160;&#160;&#160;&#160; queryString = queryString.substring(1);     <br \/>} <\/code>  <\/p>\n<p>And with that change in place, my URLs were being rewritten to automatically append the SPHostUrl parameter, and then I could navigate around my MVC application while retaining the key to the cached access token.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve put together a couple of SharePoint Apps that use MVC to run the remote web application.&#160; When you use MVC for a SharePoint remote web, Visual Studio automatically includes some helpful files to help manage the SharePoint access token.&#160; I decided to go ahead and try to use these files to help me out,&#8230;&hellip;<\/p>\n","protected":false},"author":46738,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[],"class_list":["post-8675","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/8675","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/46738"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=8675"}],"version-history":[{"count":3,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/8675\/revisions"}],"predecessor-version":[{"id":42529,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/8675\/revisions\/42529"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=8675"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=8675"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=8675"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=8675"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}