title: Cookies code outside of ActionResult
body: <p>I want to use cookies to identify users. I have learnt how to do this, but I cannot have my code in a normal method and access that from ActionResult methods. I had thought of creating an ActionResult that gets the name and then returns the real ActionResult passing the name as a parameter. But this looks messy, specially considering some ActionResult methods have parameters already!</p>
  
  <p>The first answer to <a href="https://stackoverflow.com/questions/43003165/find-user-outside-of-actionresult-methods">this question</a> looks quite promising. It proposes to create an extension method for HttpContext, which makes sense. This is my implementation:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public static class ExtensionHelper
  {
      public static string UserIdentity(this HttpContext context)
      {
          string userName;
          HttpCookie cookie = context.Request.Cookies["userNameCookie"];
          if (cookie == null)
          {
              userName = CookiesCounter.ReturnNewestID();
              cookie = new HttpCookie("userNameCookie", userName);
              cookie.Expires = DateTime.Now.AddDays(31);
              context.Response.Cookies.Add(cookie);
          }
          else
          {
              userName = cookie.Value;
          }
          return userName;
      }
  }
  </code></pre>
  
  <p>But when I call </p>
  
  <pre><code>HttpContext.UserIdentity();
  </code></pre>
  
  <p>in my controller, I get the complaint that: "HttpContextBase does not contain a definition for UserIdentity and the best extension method overload requires a receiver of type HttpContext".</p>
  
  <p>From my naive perspective (new both to extension methods and cookies) it looks like my code is a copy of that provided in the answer; what am I doing wrong? Thanks!</p>