title: MVC 5 AspNet Identity - Value cannot be null. Parameter name: source
body: <p>I am seeing a very sporadic error when users log into an MVC 5 app. It only happens once every 100 or so logins and only happens on a remote web server. When it occurs users cannot login, however, if we recycle the app pool the error goes away and everyone can login again.</p>
  
  <pre><code>Exception information: 
      Exception type: ArgumentNullException 
      Exception message: Value cannot be null.
  Parameter name: source
     at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
     at ASP._Page_Views_Shared__LoginPartial_cshtml.Execute() in c:\inetpub\wwwroot\appname\Views\Shared\_LoginPartial.cshtml:line 18
     at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
     at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
     at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
     at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData)
     at ASP._Page_Views_Shared__Layout_cshtml.Execute() in c:\inetpub\wwwroot\appname\Views\Shared\_Layout.cshtml:line 42
  </code></pre>
  
  <p>According to the above the error occurs in the LoginPartial.cshtml which is from the MVC 5 VS template but I cannot find anything there that would cause this and it doesn't make sense why this happens so sporadically and then recycling the app pool fixes it.</p>
  
  <pre><code>@model POC.Web.Models.LoginViewModel
  
  @{ ViewBag.Title = "Log in"; }
  
  &lt;h2&gt;@ViewBag.Title&lt;/h2&gt;
  &lt;div class="row"&gt;
  &lt;div class="col-md-12"&gt;
          &lt;section id="loginForm"&gt;
              @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
              {
                  @Html.AntiForgeryToken()
                  &lt;hr /&gt;
                  @Html.ValidationSummary(true)
                  &lt;div class="form-group"&gt;
                      @Html.LabelFor(m =&gt; m.UserName, new {  @class = "col-md-2 control-label" })
                      &lt;div class="col-md-10"&gt;
                          @Html.TextBoxFor(m =&gt; m.UserName, new { autocomplete = "off", @class = "form-control" })
                          @Html.ValidationMessageFor(m =&gt; m.UserName)
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      @Html.LabelFor(m =&gt; m.Password, new { @class = "col-md-2 control-label" })
                      &lt;div class="col-md-10"&gt;
                          @Html.PasswordFor(m =&gt; m.Password, new { @class = "form-control" })
                          @Html.ValidationMessageFor(m =&gt; m.Password)
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;div class="col-md-offset-2 col-md-10"&gt;
                          &lt;div class="checkbox"&gt;
                              @Html.CheckBoxFor(m =&gt; m.RememberMe)
                              @Html.LabelFor(m =&gt; m.RememberMe)
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;div class="col-md-offset-2 col-md-10"&gt;
                          &lt;input type="submit" value="Log in" class="btn btn-default" /&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;                    
              }
          &lt;/section&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  @section Scripts {
      @Scripts.Render("~/bundles/jqueryval")
  }
  </code></pre>