public UsersController(UserManager <IdentityUser> userManager, ILogger <UsersController> logger, IdentityInit identityInit)
 {
     // acquire user manager via dependency injection
     _userManager  = userManager;
     _logger       = logger;
     _identityInit = identityInit;
 }
 public SeedUsersCommandHandler(UserManager <ApplicationUser> userManager, RoleManager <IdentityRole> roleManager, IdentityInit identityInit, AppIdentityDbContext context)
 {
     _userManager  = userManager;
     _roleManager  = roleManager;
     _identityInit = identityInit;
     _context      = context;
 }
Example #3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddHttpContextAccessor();
            services.AddDbContext <ApplicationDbContext>(options =>
                                                         options.UseNpgsql(
                                                             Configuration.GetConnectionString("DefaultConnection"), b => b.MigrationsAssembly("ScadaIssuesPortal.Web")));
            services.AddIdentity <IdentityUser, IdentityRole>(options =>
            {
                options.SignIn.RequireConfirmedAccount  = false;
                options.Password.RequireDigit           = true;
                options.Password.RequireLowercase       = true;
                options.Password.RequireNonAlphanumeric = true;
                options.Password.RequireUppercase       = false;
                options.Password.RequiredLength         = 4;
                options.Password.RequiredUniqueChars    = 2;
            })
            .AddEntityFrameworkStores <ApplicationDbContext>()
            .AddDefaultTokenProviders();

            services.ConfigureApplicationCookie(options =>
            {
                // configure login path for return urls
                // https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity?view=aspnetcore-3.1&tabs=visual-studio
                options.LoginPath        = "/Identity/Account/Login";
                options.AccessDeniedPath = "/Identity/Account/AccessDenied";
            });

            // Add application services.
            services.AddTransient <IEmailSender, EmailSender>();

            // add email settings from config as a singleton service
            EmailConfiguration emailConfig = new EmailConfiguration();

            Configuration.Bind("EmailSettings", emailConfig);
            services.AddSingleton(emailConfig);

            // add folder path settings from config as a singleton service
            FolderPaths folderPaths = new FolderPaths();

            Configuration.Bind("FolderPaths", folderPaths);
            services.AddSingleton(folderPaths);

            // add super admin user details from config as a singleton service
            IdentityInit identityInit = new IdentityInit();

            Configuration.Bind("IdentityInit", identityInit);
            services.AddSingleton(identityInit);

            services.AddScoped <ICurrentUserService, CurrentUserService>();

            services
            .AddControllersWithViews()
            .AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.ContractResolver      = new CamelCasePropertyNamesContractResolver();
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            })
            .AddRazorRuntimeCompilation();
            services.AddRazorPages();
        }
        public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration, IWebHostEnvironment environment)
        {
            if (environment.IsEnvironment("Testing") || environment.IsDevelopment())
            {
                // Add Identity Infra
                services.AddDbContext <AppIdentityDbContext>(options =>
                                                             options.UseInMemoryDatabase(databaseName: "IdentityData"));
            }
            else
            {
                // Add Identity Persistence Infra
                services.AddDbContext <AppIdentityDbContext>(options =>
                                                             options.UseNpgsql(
                                                                 configuration.GetConnectionString("DefaultConnection")));
            }

            services.AddIdentity <ApplicationUser, IdentityRole>(options =>
            {
                options.SignIn.RequireConfirmedAccount        = false;
                options.Password.RequireDigit                 = true;
                options.Password.RequireLowercase             = true;
                options.Password.RequireNonAlphanumeric       = true;
                options.Password.RequireUppercase             = false;
                options.Password.RequiredLength               = 4;
                options.Password.RequiredUniqueChars          = 2;
                options.Tokens.EmailConfirmationTokenProvider = "emailconfirmation";
            })
            .AddEntityFrameworkStores <AppIdentityDbContext>()
            .AddDefaultTokenProviders()
            .AddTokenProvider <EmailConfirmationTokenProvider <ApplicationUser> >("emailconfirmation");

            // set email confirmation token lifespan to 3 days
            services.Configure <EmailConfirmationTokenProviderOptions>(opt =>
                                                                       opt.TokenLifespan = TimeSpan.FromDays(3));

            services.ConfigureApplicationCookie(options =>
            {
                // configure login path for return urls
                // https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity?view=aspnetcore-3.1&tabs=visual-studio
                options.LoginPath        = "/Identity/Account/Login";
                options.AccessDeniedPath = "/Identity/Account/AccessDenied";
            });

            // Add Email service
            services.AddTransient <IEmailSender, EmailSender>();

            // add super admin user details from config as a singleton service
            IdentityInit identityInit = new IdentityInit();

            configuration.Bind("IdentityInit", identityInit);
            services.AddSingleton(identityInit);

            // add email settings from config as a singleton service
            EmailConfiguration emailConfig = new EmailConfiguration();

            configuration.Bind("EmailSettings", emailConfig);
            services.AddSingleton(emailConfig);

            return(services);
        }
Example #5
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, UserManager <ApplicationUser> userManager, RoleManager <IdentityRole> roleManager, IdentityInit identityInit)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
            }
            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.UseAuthentication();
            app.UseAuthorization();

            // seed Users and Roles
            AppIdentityInitializer identityInitializer = new AppIdentityInitializer()
            {
                UserManager  = userManager,
                RoleManager  = roleManager,
                IdentityInit = identityInit
            };

            identityInitializer.SeedData();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
                endpoints.MapRazorPages();
            });
        }
Example #6
0
 public GetAppUserByIdQueryHandler(UserManager <ApplicationUser> userManager, IdentityInit identityInit, IMapper mapper)
 {
     _userManager  = userManager;
     _identityInit = identityInit;
     _mapper       = mapper;
 }
 public void Configuration(IAppBuilder app)
 {
     ConfigureAuth(app);
     IdentityInit.InitializeIdentityForEF();
 }
Example #8
0
 public SeedUsersCommandHandler(UserManager <ApplicationUser> userManager, RoleManager <IdentityRole> roleManager, IdentityInit identityInit)
 {
     _userManager  = userManager;
     _roleManager  = roleManager;
     _identityInit = identityInit;
 }
Example #9
0
 public CreateShiftParticipationsFromGroupCommandHandler(AppIdentityDbContext context, IdentityInit identityInit)
 {
     _context = context;
     _identityInit = identityInit;
 }