HttpContext始终为null

时间:2019-11-19 14:54:52

标签: c# asp.net razor blazor blazor-server-side

我正在做一个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);
        }
    }

0 个答案:

没有答案