title: ASP.Net Core 2.0 SignInAsync returns exception Value cannot be null, provider
body: <p>I have an ASP.Net Core 2.0 web application I am retrofitting with unit tests (using NUnit).  The application works fine, and most of the tests thus far work fine.</p>
  
  <p>However, testing the authentication/authorization (does a user get logged in and can access <code>[Authorize]</code> filtered actions) is failing with...</p>
  
  <pre><code>System.ArgumentNullException: Value cannot be null.
  Parameter name: provider
  </code></pre>
  
  <p>...after...</p>
  
  <pre><code>await HttpContext.SignInAsync(principal);
  </code></pre>
  
  <p>...but it is not clear what in fact is the underlying cause. Code execution stops in the called method here and no exception is shown in the IDE but code execution returns to the caller, then terminates (yet I still see <code>The program '[13704] dotnet.exe' has exited with code 0 (0x0).</code> in the output window of VS.)</p>
  
  <p>The Test Explorer shows red and gives the exception referenced (otherwise I would have no idea as to the problem.)</p>
  
  <p>I am working on creating a repro to point folks to (turning out to bit a bit involved thus far.)</p>
  
  <p>Does anyone know how to pinpoint the underlying cause? Is this a DI related issue (something needed that isn't being provided in the test but is in normal execution)?</p>
  
  <p><strong>UPDATE1:</strong> Providing requested authentication code...</p>
  
  <pre><code>public async Task&lt;IActionResult&gt; Registration(RegistrationViewModel vm) {
      if (ModelState.IsValid) {
          // Create registration for user
          var regData = createRegistrationData(vm);
          _repository.AddUserRegistrationWithGroup(regData);
  
          var claims = new List&lt;Claim&gt; {
              new Claim(ClaimTypes.NameIdentifier, regData.UserId.ToString())
          };
          var ident = new ClaimsIdentity(claims);
          var principal = new ClaimsPrincipal(ident);
  
          await HttpContext.SignInAsync(principal); // FAILS HERE
  
          return RedirectToAction("Welcome", "App");
      } else {
          ModelState.AddModelError("", "Invalid registration information.");
      }
  
      return View();
  }
  </code></pre>
  
  <p>The test code that fails...</p>
  
  <pre><code>public async Task TestRegistration()
  {
      var ctx = Utils.GetInMemContext();
      Utils.LoadJsonData(ctx);
      var repo = new Repository(ctx);
      var auth = new AuthController(repo);
      auth.ControllerContext = new ControllerContext();
      auth.ControllerContext.HttpContext = new DefaultHttpContext();
  
      var vm = new RegistrationViewModel()
      {
          OrgName = "Dev Org",
          BirthdayDay = 1,
          BirthdayMonth = "January",
          BirthdayYear = 1979 
      };
  
      var orig = ctx.Registrations.Count();
      var result = await auth.Registration(vm); // STEPS IN, THEN FAILS
      var cnt = ctx.Registrations.Count();
      var view = result as ViewResult;
  
      Assert.AreEqual(0, orig);
      Assert.AreEqual(1, cnt);
      Assert.IsNotNull(result);
      Assert.IsNotNull(view);
      Assert.IsNotNull(view.Model);
      Assert.IsTrue(string.IsNullOrEmpty(view.ViewName) || view.ViewName == "Welcome");
  }
  </code></pre>
  
  <p><strong>UPDATE3:</strong> Based on <a href="https://chat.stackoverflow.com/rooms/165687/discussion-between-t-j-and-nkosi">chat</a> @nkosi <a href="https://chat.stackoverflow.com/transcript/message/41366788#41366788">suggested</a> that this is a problem stemming from my not fulfilling the needs of the dependency injection requirements for <code>HttpContext</code>.  </p>
  
  <p><a href="https://chat.stackoverflow.com/transcript/message/41367614#41367614">However</a>, what isn't yet clear is: if it is, in fact, an issue of not providing the proper service dependency, why does the code work normally (when not being tested).  The SUT (controller) only accepts an IRepository parameter (so that is all that is provided in any case.)  Why create an overloaded ctor (or mock) just for test, when the existing ctor is all that is called when running the program and it runs without issue?</p>
  
  <p><strong>UPDATE4</strong>: While @Nkosi answered the bug/problem with a solution, I am still wondering why the IDE isn't accurately/consistently presenting the underlying exception.  Is this a bug, or due to the async/await operators and the NUnit Test Adapter/runner? Why aren't exceptions "popping" like I would expect while debugging the test, and the exit code is still zero (typically indicating a successful return state)?</p>