我正在做一个Blazor服务器端项目,我需要将HttpContext
放入我的SignInService
中,以便SignInManager
服务可以登录用户。我这样更新了Startup.cs
文件:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddScoped<LogInService>();
services.AddScoped<SignInManager<IdentityUser>>();
services.AddHttpContextAccessor(); //Line added
}
但是,无论是在客户端还是在服务器端,HttpContextAccessor.HttpContext
始终为空。
为什么这样?我错过了创业公司的东西吗?通常,我应该通过依赖项注入services.AddHttpContextAccessor();
来获取上下文,但是它不会发生。
编辑::这是SignInService
,需要使用HttpContext
:
public class SignInService
{
private readonly SignInManager<IdentityUser> _signInManager;
public SignInService(SignInManager<IdentityUser> signInManager)
{
_signInManager = signInManager;
}
public async Task<SignInResult> TrySignIn(LogInForm logInForm)
{
return await _signInManager.PasswordSignInAsync(logInForm.Mail, logInForm.Password, false, false);
}
}