title: Website Slow At Loading The Login Form
body: <p>I have an ASP.NET MVC website which I am developing, and I recently deployed it on a test server so the client could give some initial feedback.</p>
  
  <p>I have noticed that on the live server there is a considerable delay in loading the login form, of around 20-30 seconds. Once you are logged in, the system functions just fine, and is responsive.</p>
  
  <p>If you log out, and return to the login page, it is once again slow. </p>
  
  <p>It doesn't appear to be an issue with the apppool spooling up, as it happens every time on the page, not just once, and all items appear to be loading.</p>
  
  <p>Any suggestions on how to debug this?</p>
  
  <p>Below is the BaseController from which all controllers enherit, plus the account login controller.</p>
  
  <pre><code>protected override void ExecuteCore()
      {
  
  
  
          if (User.Identity.IsAuthenticated)
          {
              try
              {
                  AccountDataContext = new AccountDAL.DataContext(ConfigurationManager.AppSettings["Server"]);
                  // set the current user. 
                  CurrentUser = AccountDataContext.Users.FirstOrDefault(x =&gt; x.Email == User.Identity.Name);
                  AccountDataContext.CurrentAccount = CurrentUser.Account;
                  ViewBag.CurrentUser = CurrentUser;
                  ViewBag.Account = CurrentUser.Account;
  
                  SystemDataContext = new SystemDAL.DataContext(ConfigurationManager.AppSettings["Server"], CurrentUser.Account.Database);
  
                  // setup the account based on the users settings
                  ViewBag.Theme = "Default"; // hard coded for now
              }
              catch (Exception)
              {
                  // if the previous threw an exception, then the logged in user has been deleted
                  // log them out
                  FormsAuthentication.SignOut();
                  Session.Abandon();
  
                  // clear the authentication cookie
                  var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, "");
                  cookie.Expires = DateTime.Now.AddYears(-1);
                  Response.Cookies.Add(cookie);
  
                  FormsAuthentication.RedirectToLoginPage();
              }
          }
  
          base.ExecuteCore();
      }
  </code></pre>
  
  <p>and the Accounts controller:</p>
  
  <pre><code>    [AllowAnonymous]
      public ActionResult Login(string returnUrl)
      {
          ViewBag.ReturnUrl = returnUrl;
          return View();
      }
  
      //
      // POST: /Account/Login
      [HttpPost]
      [AllowAnonymous]
      [ValidateAntiForgeryToken]
      public ActionResult Login(LoginViewModel model, string returnUrl)
      {
          if (ModelState.IsValid)
          {
  
              if(AccountDataContext == null)
                  AccountDataContext = new AccountDAL.DataContext(ConfigurationManager.AppSettings["Server"]);
  
              var user = AccountDataContext.Users.FirstOrDefault(x =&gt; x.Email == model.UserName &amp;&amp; x.Password == model.Password);
              if (user != null)
              {
                  FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                  return RedirectToLocal(returnUrl);
              }
              else
              {
                  ModelState.AddModelError("", "Invalid username or password.");
              }
          }
  
          // If we got this far, something failed, redisplay form
          return View(model);
      }
  </code></pre>