title: C#.NET Using isAuthenticated
body: <p>Im using the MVC format to create a website. Right now all it does is manage users from an SQL server. What i'm trying to do now is have the user log in and then be able to manage the Users. From the Login page it should go to the Index of the Account but I only want this page to be viewable by authenticated users. It works fine if I: </p>
  
  <p>1)set the function in the controler to [AllowAnonymous] (This is not what i want)</p>
  
  <p>2)Allow Windows Authentication (Which is not what I want because once I deploy, it'll be on the web)</p>
  
  <p>It really just boils down to how do I authenticate a user and then have that authentication persist. </p>
  
  <p>Here is the login page:</p>
  
  <pre><code>@model myWebsite.Models.LoginModel
  
  @{
      ViewBag.Title = "Login";
      ViewBag.ReturnUrl = "Index";
  }
  
  &lt;h2&gt;Login&lt;/h2&gt;
  
  @using (Html.BeginForm("Login", "Login", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
  {
      @Html.AntiForgeryToken()
      &lt;div class="form-horizontal"&gt;
          &lt;h4&gt;Login&lt;/h4&gt;
          &lt;hr /&gt;
          @Html.ValidationSummary(true, "", new { @class = "text-danger"})
          &lt;div class="form-group"&gt;
              @Html.LabelFor(Model =&gt; Model.UserName, new { @class = "control-label col-md-2"})
              &lt;div class="col-md-10"&gt;
                  @Html.TextBoxFor(Model =&gt; Model.UserName, new { @class = "col-md-2 control-label"})
                  @Html.ValidationMessageFor(Model =&gt; Model.UserName, "" , new { @class = "text-danger"})
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              @Html.LabelFor(Model =&gt; Model.Password, new { @class = "control-label col-md-2"})
              &lt;div class="col-md-10"&gt;
                  @Html.TextBoxFor(Model =&gt; Model.Password, new { @class = "col-md-2 control-label"})
                  @Html.ValidationMessageFor(Model =&gt; Model.Password, "" , new { @class = "text-danger"})
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;input type="submit" value="Log In" class="btn btn-default" /&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  }
  </code></pre>
  
  <p>This is the partial portion of every page</p>
  
  <pre><code>@using Microsoft.AspNet.Identity;
  
  @if (Request.IsAuthenticated)
  {
      using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
      {
          @Html.AntiForgeryToken()
          &lt;ul class="nav navbar-nav navbar-right"&gt;
              &lt;li&gt;@Html.ActionLink("Hello "  + User.Identity.GetUserName() + "!", "Index" , "Manage", routeValues: null, htmlAttributes: new { title = "Manage" } )&lt;/li&gt;
          &lt;/ul&gt;
      }
  }
  else
  {
      &lt;ul class="nav navbar-nav navbar-right"&gt;
          &lt;li&gt;@Html.ActionLink("Register", "Create", "Login", routeValues: null, htmlAttributes: new { id = "registerLink" })&lt;/li&gt;
          &lt;li&gt;@Html.ActionLink("Log in", "Login", "Login", routeValues: null, htmlAttributes: new { id = "loginLink" })&lt;/li&gt;
      &lt;/ul&gt;
  }
  </code></pre>
  
  <p>This is the controller </p>
  
  <pre><code>    [AllowAnonymous]
      // GET: Login
      public ActionResult Login()
      {
          return View();
      }
  
  
      [AllowAnonymous]
      // GET: Login
      public ActionResult Login()
      {
          return View();
      }
  
      [HttpPost]
      [AllowAnonymous]
      [ValidateAntiForgeryToken]
      public ActionResult Login(LoginModel model, string retunUrl)
      {
  
          /* 
          if (!ModelState.IsValid)
          {
              Console.WriteLine("IS NOT VALID");
              return View(model);
          }
         */
          String UserName = model.UserName;
          String Password = model.Password;
  
          LoginContext LC = new LoginContext();
          LoginModel ValidUser = LC.UserList.Single(Person =&gt; Person.UserName == UserName &amp;&amp; Person.Password == Password);
  
          if (ValidUser != null)
          {
              return Redirect("Index");
          }
          return View(model);
      }
  
  
  
  
      // GET: Login Index of users
      [AllowAnonymous]
      public ActionResult Index()
      {
          return View(db.UserList.ToList());
      }
  </code></pre>