Ejemplo n.º 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var jwtOptions = services.ConfigureOption <JwtOptions>(Configuration);

            services.ConfigureOption <Common.Options.ImageOptions>(Configuration);
            services.ConfigureOption <DbSeedOptions>(Configuration);

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(options =>
            {
                options.TokenValidationParameters = JwtHelper.CreateTokenValidationParameters(jwtOptions);
            });

            services.AddDbContext <ImageStoreDbContext>(builder =>
            {
                builder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), o => o.CommandTimeout(300));
            });

            services.AddTransient <JwtHelper>();
            services.AddTransient <AccountService>();
            services.AddTransient <IHttpContextAccessor, HttpContextAccessor>();
            services.AddTransient(s =>
            {
                IHttpContextAccessor contextAccessor = s.GetService <IHttpContextAccessor>();
                ClaimsPrincipal user = contextAccessor?.HttpContext?.User;
                return(user ?? throw new Exception("User not resolved"));
            });

            services.AddIdentity <User, IdentityRole <int> >(opt =>
            {
                opt.Password.RequireDigit           = false;
                opt.Password.RequireLowercase       = true;
                opt.Password.RequireUppercase       = true;
                opt.Password.RequiredLength         = 4;
                opt.Password.RequireNonAlphanumeric = false;
                opt.Password.RequiredUniqueChars    = 1;
            })
            .AddRoles <IdentityRole <int> >()
            .AddEntityFrameworkStores <ImageStoreDbContext>()
            .AddDefaultTokenProviders();

            services.AddControllers()
            .AddFluentValidation(fv =>
            {
                fv.RegisterValidatorsFromAssemblyContaining <LoginUserDtoValidator>();
                fv.ImplicitlyValidateChildProperties = true;
            });

            services.AddProblemDetails(options =>
            {
                options.IncludeExceptionDetails     = (ctx, ex) => Environment.IsDevelopment();
                options.ShouldLogUnhandledException = (ctx, ex, details) => true;

                options.Map <ImageStoreException>(ex => new StatusCodeProblemDetails(ex.StatusCode));
            });

            services.AddSpaStaticFiles(configuration => configuration.RootPath = "wwwroot");

            services.AddSwaggerDocument(settings =>
            {
                settings.Title = "ImageStore";
                settings.DefaultReferenceTypeNullHandling         = NJsonSchema.Generation.ReferenceTypeNullHandling.NotNull;
                settings.DefaultResponseReferenceTypeNullHandling = NJsonSchema.Generation.ReferenceTypeNullHandling.NotNull;
            });

            services.AddAutoMapper(
                typeof(UserProfile)
                );

            services.AddScoped <IAccountService, AccountService>();
            services.AddScoped <IImageService, ImageService>();
        }