title: Authentication in ASP.NET MVC
body: <p>I begin with ASP.NET and I have a problem with authentication.</p>
  
  <p>This is the code:</p>
  
  <p>web.config </p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;system.web&gt;
    &lt;authentication mode="Forms"&gt;
      &lt;forms loginUrl="~/users/login" timeout="3000"  /&gt;
    &lt;/authentication&gt;
    &lt;compilation debug="true" targetFramework="4.5.2" /&gt;
    &lt;httpRuntime targetFramework="4.5.2" /&gt;
  &lt;/system.web&gt;
  </code></pre>
  
  <p>login</p>
  
  <pre class="lang-cs prettyprint-override"><code>public ActionResult login()
  {
      if (Request.HttpMethod == "POST")
      {
          if (Request.Form["email"] == null || Request.Form["password"] == null)
              ViewData["error"] = "form_error";
          else
          {
              User user = this.dal.authUser(
              Request.Form["email"],
              Request.Form["password"]);
              if (user == null)
                  ViewData["error"] = "auth_error";
              else
              {
                  FormsAuthentication.SetAuthCookie(user.id.ToString(), false);
                  return Redirect("/profiles/" + user.id);
              }
          }
      }
      return (View());
  }
  </code></pre>
  
  <p>profile controller</p>
  
  <pre class="lang-cs prettyprint-override"><code>[HttpGet]
  public ActionResult get(int id)
  {
      ViewData["auth"] = false;
      if (HttpContext.User.Identity.IsAuthenticated)
          ViewData["auth"] = true;
      Response.Write(HttpContext.User.Identity.Name);
      Profile profile = this.dal.getProfile(id);
      if (profile == null)
          return View("~/Views/error404.cshtml");
      ViewData["profile"] = profile;
      return View();
  }
  </code></pre>
  
  <p>and View for get a profile</p>
  
  <pre><code>@using Plume.Areas.Users.Models;
  
  @{
      Layout = "~/Views/Layout/layout.cshtml";
      Variables.pageTitle = "Profil";
      Profile profile = (Profile)ViewData["profile"];
      bool auth = (bool)ViewData["auth"];
  }
  
  @{ 
      if (auth)
      {
          &lt;h1&gt;Auth&lt;/h1&gt;
      }
  }
  
  &lt;p&gt;
      ceci est le profil de : 
      @profile.username
      &lt;br /&gt;
      dont l'email est :
      @profile.user.email
  &lt;/p&gt;
  </code></pre>
  
  <p>So, when I log, a cookie called <code>.ASPXAUTH</code> is returned, but in the view for a profile, the <code>h1</code> isn't displayed.</p>
  
  <p>What is not correct?</p>