【发布时间】:2021-03-04 16:27:37
【问题描述】:
尝试运行一些集成测试并在我的单元和集成测试中使用 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 提供程序)
【问题讨论】:
-
您可以看到这个thread 可能会有所帮助。
-
是的,这行得通。非常感谢。 @Yingiu 请发表您的评论作为答案。
标签: c# .net asp.net-core