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 jwtBearerConfig = Configuration.GetSection("JwtBearer").Get <JwtBearerConfig>();

            services.AddSingleton <IJwtService>(new JwtService(jwtBearerConfig));

            var database = new MongoBase();

            if (!database.OpenConnection())
            {
                throw new Exception("Could not connect to database");
            }
            services.AddSingleton(database);

            services.AddScoped <IUserService, UserService>();
            services.AddScoped <IBookService, BookService>();
            services.AddScoped <ILendService, LendService>();
            services.AddScoped <IReservationService, ReservationService>();

            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(options => Configuration.Bind("CookieConfig", options))
            .AddJwtBearer(options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidateIssuer   = true,
                    ValidIssuer      = jwtBearerConfig.Issuer,
                    ValidateAudience = true,
                    ValidAudience    = jwtBearerConfig.Audience,
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtBearerConfig.Secret)),
                    ValidateLifetime = true,
                    NameClaimType    = "name"
                };
            });

            services.AddControllersWithViews();
        }