Asp.Net Web Forms Mixed with .Net Core 2.2, IHttpContextAccessor.HttpContext is null

Viewed 169

I am trying to use .Net Core 2.2 Dependency Injection and Asp.Net Core 2.2 Identity in an old Asp.Net web forms app, I setup dependency injection correctly. The core Identity also works when I created a user name and password using UserManager.

But My problem is SignInManager, since it needs a Microsoft.AspNetCore.Http.HttpContext instance so that principals are set.

My startup is:

    [assembly: OwinStartupAttribute(typeof(WebForms22.Startup))]
namespace WebForms22
{
    public partial class Startup {

        public static IServiceProvider Services { get; set; }        
        
        public void Configuration(IAppBuilder app) 
        {           
            var services = new ServiceCollection();
            //ConfigureAuth(app);
            ConfigureServices(services);
            Services = services.BuildServiceProvider();

        }

        public void ConfigureServices(IServiceCollection services)
        {           
            services.AddDbContext<IdentityContext>(options =>
               options.UseSqlServer(
                   @"Server=localhost;Database=WebForms22;Trusted_Connection=True;MultipleActiveResultSets=true;"), ServiceLifetime.Transient);

            services.AddIdentity<AppUser, IdentityRole>(opts =>
            {
                opts.User.RequireUniqueEmail = true;
                //opts.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyz";
                opts.Password.RequiredLength = 6;
                opts.Password.RequireNonAlphanumeric = false;
                opts.Password.RequireLowercase = false;
                opts.Password.RequireUppercase = false;
                opts.Password.RequireDigit = false;
                opts.ClaimsIdentity.UserIdClaimType = JwtRegisteredClaimNames.Sub; //commented      
                opts.Tokens.EmailConfirmationTokenProvider = "custom";
            }).AddEntityFrameworkStores<IdentityContext>().AddDefaultTokenProviders()
            .AddTokenProvider<DataProtectorTokenProvider<AppUser>>("custom");

            services.AddHttpContextAccessor();
            //services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            services.AddTransient<DAL.Business.Account>();
            services.AddTransient<DAL.Business.Admin>();
            services.AddTransient<DAL.Business.Home>();
        }
      
    }
}

And this is the Login method in Login.aspx.cs:

protected async void LogIn(object sender, EventArgs e)
        {
            if (IsValid)
            {
                // Validate the user password
                var manager = Startup.Services.GetRequiredService<UserManager<AppUser>>(); 
                var signinManager = Startup.Services.GetRequiredService<SignInManager<AppUser>>();  

                AppUser user = await manager.FindByNameAsync(Email.Text);
                change to shouldLockout: true

                var context = Startup.Services.GetRequiredService<IHttpContextAccessor>().HttpContext;  //<< Here              
                signinManager.Context = context; // == null       

                Microsoft.AspNetCore.Identity.SignInResult result = 
                  await signinManager.PasswordSignInAsync(user, Password.Text, RememberMe.Checked, true);                       
                
                if (result.Succeeded)
                {...

But I get this error when I want to Login:

System.InvalidOperationException: 'HttpContext must not be null.'

How do I set Asp.Net core HttpContext during signin?

EDIT: HttpContext in

Startup.Services.GetRequiredService<IHttpContextAccessor>().HttpContext;

is always null

0 Answers
Related