title: ASP.NET MVC 3 using Authentication
body: <p>How can I save something using FormsAuthentication? I don't want to store UserId through URL's.</p>
  <p>For example, now I have this code:</p>
  <pre><code>//UserController class:
  [HttpPost]
  public ActionResult LogOn(LogOnModel model, string returnUrl)
  {
  if (ModelState.IsValid)
  {
    if (repository.ValidateUser(model.Login, model.Password))
    {
      FormsAuthentication.SetAuthCookie(model.Login, model.RememberMe);
      if (Url.IsLocalUrl(returnUrl))
      {
        return Redirect(returnUrl);
      }
      else
      {
        return RedirectToAction(&quot;Project&quot;, &quot;Index&quot;);
      }
    }
    else
    {
       ModelState.AddModelError(&quot;&quot;, &quot;Incorrect name or password.&quot;);
    }
  }
  
  return View(model);
  }
  </code></pre>
  <p><code>ProjectController</code> class:</p>
  <pre><code>public ViewResult Index()
  {
      return View(repository.GetUserProjects(
          this.ControllerContext.HttpContext.User.Identity.Name));
  }
  </code></pre>
  <p><code>ProjectRepository</code>:</p>
  <pre><code>ProjectsContext context = new ProjectsContext();
  UsersContext uCnt = new UsersContext();
  
  public IEnumerable&lt;Project&gt; GetUserProjects(String username)
  {
      if (String.IsNullOrEmpty(username))
          throw new ArgumentNullException(&quot;username&quot;, &quot;Login is empty&quot;);
      return this.uCnt.Users
                 .FirstOrDefault(u =&gt; u.Login == username)
                 .Projects
                 .ToList();
  }
  </code></pre>
  <p>ProjectController and ProjectRepository don't looks like good code... Maybe someone can give advise, how to store UserID without using URL's? Best way to do this is save IDs on autorisation, I think. I don't found any properties in User.Identity to do this...</p>
  <h1>UPD</h1>
  <p>I beg a pardon, but I forgot to say that I'm using MVC-3 with Razor view.
  And that UserId is not a string (User.Identity.Name is a string) it could be GUID or maybe my own object...</p>