title: MVC save a token in session
body: <p>I am calling a service, and I need to pass the user's permanent security token with every request I make.  </p>
  
  <p>In order to do that, I've added this method to my base controller class:  </p>
  
  <pre><code>protected UserData getUsr()
  {
          try
          {
              UserData usr = new UserData();
              usr.SecurityToken = Session["secToken"].ToString();
  
              MembershipUser mvcUser = Membership.GetUser(HttpContext.User.Identity.Name);
              usr.Id = (int)mvcUser.ProviderUserKey;
  
              return usr;
          }
          catch (Exception ex)
          {
              log.Debug("Could not create usr object", ex);
              throw new Exception("Could not authenticate");
          }
      }
  </code></pre>
  
  <p>This issue here is that sometimes the User.Identity data out-lasts the session data, causing weird bugs to happen with the user seeing they are logged in but then their requests failing.  </p>
  
  <p>Is there a better way to store this token/can I store it in such a way that it will expire whenever the User.Identity object expires? </p>
  
  <p>Also, if anyone knows of some good basic understanding examples/documentation for HttpContext and MVC authorize filters that would be great.</p>