Example #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            string conn = Configuration.GetConnectionString("DefaultConnection");

            if (conn.Contains("%CONTENTROOTPATH%"))
            {
                conn = conn.Replace("%CONTENTROOTPATH%", _contentRootPath);
            }
            DbConfig.ConfigureDb(services, conn);
            RepositoriesConfig.ConfigureRepositories(services);
            ServicesConfig.ConfigureServices(services);

            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            services.Configure <AuthTokenOptions>(Configuration.GetSection("Authorization:Jwt"));
            var authTokenOptions = services.BuildServiceProvider().GetService <IOptions <AuthTokenOptions> >().Value;

            services.AddIdentity <ApplicationUser, IdentityRole>(options =>
            {
                options.Lockout.DefaultLockoutTimeSpan  = TimeSpan.FromMinutes(10);
                options.Lockout.MaxFailedAccessAttempts = 5;
                options.Password.RequireDigit           = false;
                options.Password.RequireLowercase       = false;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequireUppercase       = false;
            })
            .AddEntityFrameworkStores <SimContext>()
            .AddDefaultTokenProviders();

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultScheme             = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(cfg =>
            {
                cfg.RequireHttpsMetadata      = false;
                cfg.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer           = true,
                    ValidIssuer              = authTokenOptions.Issuer,
                    ValidateAudience         = true,
                    ValidAudience            = authTokenOptions.Audience,
                    ValidateLifetime         = true,
                    IssuerSigningKey         = authTokenOptions.GetSymmetricSecurityKey(),
                    ValidateIssuerSigningKey = true,
                    ClockSkew = TimeSpan.Zero
                };
            });


            services.AddControllersWithViews();
            // In production, the Angular files will be served from this directory
            services.AddSpaStaticFiles(configuration => { configuration.RootPath = "ClientApp/dist"; });
        }
Example #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            var conn = Configuration.GetConnectionString("DefaultConnection");

            if (conn.Contains("%CONTENTROOTPATH%"))
            {
                conn = conn.Replace("%CONTENTROOTPATH%", _contentRootPath);
            }

            //ConnectionConfig.SetConnString(Configuration.GetConnectionString("DefaultConnection"));

            services.AddRazorPages().AddRazorPagesOptions(options =>
            {
                options.Conventions.AddAreaPageRoute("Identity", "/Account/Login", "/login");
            });
            services.AddServerSideBlazor();
            services
            .AddScoped <AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider <IdentityUser>
                        >();
            DbConfig.ConfigureDb(services, conn);
            RepositoriesConfig.ConfigureRepositories(services);
            ServicesConfig.ConfigureServices(services);

            services.AddDefaultIdentity <ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
            .AddEntityFrameworkStores <DataContext>();
            services.AddHttpContextAccessor();

            // services.AddIdentity<ApplicationUser, IdentityRole>(options =>
            //     {
            //         options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(10);
            //         options.Lockout.MaxFailedAccessAttempts = 5;
            //         options.Password.RequireDigit = false;
            //         options.Password.RequireLowercase = false;
            //         options.Password.RequireNonAlphanumeric = false;
            //         options.Password.RequireUppercase = false;
            //     })
            //     .AddEntityFrameworkStores<DataContext>()
            //     .AddDefaultTokenProviders();
        }