尝试运行一些集成测试并在我的单元和集成测试中使用 DefaultHttpContext。有没有办法在 DefaultHttpContext 上调用 SignInAsync()?
var claims = new List<Claim>();
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var authProperties = new AuthenticationProperties
{
AllowRefresh = true,
IsPersistent = true,
IssuedUtc = DateTime.UtcNow
};
await _controller.HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(claimsIdentity),
authProperties);
由于某种原因,在使用 DefaultHttpContext 时,在上面运行会引发异常:
<块引用>System.ArgumentNullException:值不能为空。 (参数'提供者') 在 Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)