title: How do i keep the Sharepoint context when moving around an ASP.NET MVC application without using the query string?
body: <p>I'm building a small application in MVC 4.5. I've got an Azure database, and i'm using code first with the Entity framework to set it up. The app is hosted on my development sharepoint area.</p>
  
  <p>The Home controller's <code>Index()</code> Action has the <code>[SharePointContextFilter]</code> and loads, among other things, the username of the logged in user. When the application is debugged and this first action runs, the Sharepoint <code>{StandardTokens}</code> get appended to the url, so <code>SPHostUrl</code> and <code>AppWebUrl</code> and a few other variables get added to the query string.</p>
  
  <p>If i navigate away to an action without the <code>[SharePointContextFilter]</code> it works fine, until i navigate back to the action <em>with</em> the <code>[SharePointContextFilter]</code>. Then i get an error saying:</p>
  
  <pre><code>Unknown User
  Unable to determine your identity. Please try again by launching the app installed on your site.
  </code></pre>
  
  <p>I assume this is because a few of the Sharepoint <code>{StandardTokens}</code> are missing, because if i manually append them to the link like so:</p>
  
  <pre><code>@Url.Action("Index", "Home", new { SPHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Current.Request).AbsoluteUri })
  </code></pre>
  
  <p>and mark the other action with the <code>[SharePointContextFilter]</code> as well, it still works.</p>
  
  <p><em>Hovever</em> this seems like a needlessly complex way to solve this problem. I don't want to mark every single action in my app with the <code>[SharePointContextFilter]</code>, and manually insert the <code>{StandardTokens}</code> into the query string for every link i create. Shouldn't it be possible to save this information to session or a cookie in some way, so i don't have to do this?</p>
  
  <p>For reference, here is some code:</p>
  
  <p>HomeController.Index(), the first Action that is run.</p>
  
  <pre><code>    [SharePointContextFilter]
      public ActionResult Index()
      {
          User spUser = null;
  
          var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
  
          using (var clientContext = spContext.CreateUserClientContextForSPHost())
          {
              if (clientContext != null)
              {
                  spUser = clientContext.Web.CurrentUser;
  
                  clientContext.Load(spUser, user =&gt; user.Title);
  
                  clientContext.ExecuteQuery();
  
                  ViewBag.UserName = spUser.Title;
              }
          }
  
          return View();
      }
  </code></pre>
  
  <p>Here is the <code>[SharePointContextFilter]</code> attribute (generated by visual studio):</p>
  
  <pre><code>/// &lt;summary&gt;
  /// SharePoint action filter attribute.
  /// &lt;/summary&gt;
  public class SharePointContextFilterAttribute : ActionFilterAttribute
  {
      public override void OnActionExecuting(ActionExecutingContext filterContext)
      {
          if (filterContext == null)
          {
              throw new ArgumentNullException("filterContext");
          }
  
          SharePointContext currentContext = SharePointContextProvider.Current.GetSharePointContext(filterContext.HttpContext);
  
          Uri redirectUrl;
          switch (SharePointContextProvider.CheckRedirectionStatus(filterContext.HttpContext, out redirectUrl))
          {
              case RedirectionStatus.Ok:
                  return;
              case RedirectionStatus.ShouldRedirect:
                  filterContext.Result = new RedirectResult(redirectUrl.AbsoluteUri);
                  break;
              case RedirectionStatus.CanNotRedirect:
                  filterContext.Result = new ViewResult { ViewName = "Error" };
                  break;
          }
      }
  }
  </code></pre>
  
  <p>The links that i use. From the _Layout.cshtml file.:</p>
  
  <pre><code>&lt;li id="Home"&gt;&lt;a href="@Url.Action("Index", "Home", new { SPHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Current.Request).AbsoluteUri })"&gt;Home&lt;/a&gt;&lt;/li&gt;
  &lt;li id="Contract"&gt;&lt;a href="@Url.Action("Index", "Contract", new { SPHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Current.Request).AbsoluteUri })"&gt;Avrop&lt;/a&gt;&lt;/li&gt;
  </code></pre>
  
  <p>If i try to use these links from an Action that <em>isn't</em> marked with the <code>[SharePointContextFilter]</code> filter, the <code>SPHostUrl</code> isn't found. If i try to link to an Action which <em>is</em> marked with the <code>[SharePointContextFilter]</code> filter, i get the aforementioned error if the <code>SPHostUrl</code> isn't included.</p>
  
  <p>This basically creates a situation where i can navigate away from the filtered actions, but then i can never return to them.</p>
  
  <p>I hope this was clear enough.</p>