¿Cómo puedo hacer para recuperar información del usuario logeado en un controlador si estoy usando IdentityUser?
¿Cómo lo puedo configurar en la clase StartUp.cs?
Mi código:
private readonly UserManager<ApplicationUser> userManager;
public TestController(UserManager<ApplicationUser> _userManager)
{
userManager = _userManager;
}
Me devuelve el error:
InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`while attempting to activate 'IntegracionDocu.Controllers.TestController'.
StartUp.cs:
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.AddTransient<IHttpContextAccessor,HttpContextAccessor>();
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(1);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
services.AddControllersWithViews();
services.AddRazorPages();
//*****************************************************************************
services.AddTransient<IEmailSender, EmailSender>();
services.AddTransient<ApplicationDbContext, ApplicationDbContext>();
services.AddTransient<IReadFile, ReadFile>();
services.AddTransient<IPaginacion, Paginacion>();
services.AddTransient<IPeticiones, Peticiones>();
services.AddTransient<IMetodosFtp, MetodosFtp>();
services.AddTransient<IStockRepository, StockRepository>();
services.AddTransient<IMailingRepository, MailingRepository>();
services.AddTransient<IFunctionApplication, FunctionApplication>();
services.AddTransient<IAspNetUsersRepository, AspNetUsersRepository>();
}
UserManager<ApplicationUser>
en el Controller, pero no veo esa dependencia agregada en elStartup.cs
. Dónde está?