Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
- {
- private string _userName;
- public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options, UserResolverService userService)
- : base(options)
- {
- _userName = userService.GetActualUser();
- }
- protected override void OnModelCreating(ModelBuilder builder)
- {
- base.OnModelCreating(builder);
- }
- }
- public class UserResolverService
- {
- private readonly IHttpContextAccessor _accessor;
- public UserResolverService(IHttpContextAccessor accessor)
- {
- _accessor = accessor;
- }
- public string GetActualUser()
- {
- if(_accessor.HttpContext.User.Identity.IsAuthenticated == true)
- {
- return _accessor.HttpContext.User?.Identity?.Name;
- }
- var dummy = "Dummy";
- return dummy;
- //
- }
- }
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddDbContext<ApplicationDbContext>(options =>
- options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
- services.AddIdentity<ApplicationUser, IdentityRole>()
- .AddEntityFrameworkStores<ApplicationDbContext>()
- .AddDefaultTokenProviders();
- // Add application services.
- services.AddTransient<IEmailSender, EmailSender>();
- services.AddTransient<IHttpContextAccessor, HttpContextAccessor>();
- services.AddTransient<UserResolverService>();
- services.AddMvc();
- }
Add Comment
Please, Sign In to add comment