Esempio n. 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            //Options Pattern Implementation
            //Configure AppSettings section
            var appConfig = Configuration.GetSection("AppSettings");

            services.Configure <AppSettings>(appConfig);

            //Using AppSettings
            var    appSettings      = appConfig.Get <AppSettings>();
            string connectionString = appSettings.NoteAppConnectionString;



            //Read a value from the configuration file appSettings.json
            //string hosts = Configuration.GetValue<string>("AllowedHosts");



            //Jwt token authentication configuration
            var secret = Encoding.ASCII.GetBytes(appSettings.Secret);

            services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(x =>
            {
                x.RequireHttpsMetadata      = false;
                x.SaveToken                 = true;
                x.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(secret),
                    ValidateIssuer           = false,
                    ValidateAudience         = false
                };
            });



            //Call the RegisterModule method that register all the repositories and the NoteDbContext class
            DIModule.RegiseterModule(services, connectionString);

            //Registering services
            services.AddTransient <IUserService, UserService>();
            services.AddTransient <INoteService, NoteService>();
        }