Estoy tratando de establecer una variable de sesión accediendo al HttpContext
HttpContext.Session.SetString("Empresa", "RACE");
pero me devuelve el siguiente error : Microsoft.AspNetCore.Mvc.ControllerBase.HttpContext.get devolvió null.
¿Cómo puedo resolver esto?
controller:
private readonly IHttpContextAccessor httpContext;
public PeticionesController(IReadFile _readFile, IMailingRepository _mailingRepository, IPeticiones _peticiones,IPaginacion _paginacion, IFunctionApplication _funcion, IHttpContextAccessor _httpContext)
{
readFile = _readFile;
mailingRepository = _mailingRepository;
peticiones = _peticiones;
paginacion = _paginacion;
funcion = _funcion;
httpContext = _httpContext;
MockUpUser();
}
private void MockUpUser()
{ //AQUI EL ERROR
HttpContext.Session.SetString("Id", "9f1885b0bf-07a9-40dc-b226-285c862b04c22");
HttpContext.Session.SetString("IdEmpresa", "Z123456");
HttpContext.Session.SetString("Empresa", "RACEXX");
HttpContext.Session.SetString("RoleId", "administrador");
HttpContext.Session.SetString("RoleName", "administrador");
}
StartUp.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDatabaseDeveloperPageExceptionFilter();
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddControllersWithViews();
services.AddSingleton<Microsoft.AspNetCore.Http.IHttpContextAccessor, Microsoft.AspNetCore.Http.HttpContextAccessor>();
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromSeconds(10);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
services.AddControllersWithViews();
services.AddRazorPages();
services.AddTransient<ApplicationDbContext, ApplicationDbContext>();
Peticiones.peticionesSize = Configuration.GetValue<int>("PageSize:PeticionesSize");
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseSession();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
}
}
}
MockUpUser
en tu propio constructor, si quieresMockUpUser
quieres que sea algo general, usa inyeccion de dependencias