Exemple #1
0
        // This method gets called by the runtime. Use this method to add services to the container
        public void ConfigureServices(IServiceCollection services)
        {
            string connectionString        = Configuration.GetConnectionString("RecipeBookDbConnection");
            var    connectionStringBuilder = new SqlConnectionStringBuilder(connectionString);

            connectionStringBuilder.DecodePasswordAndUserId();
            services.AddDbContext <RecipeBookContext>(options => options.UseSqlServer(connectionStringBuilder.ConnectionString));

            var secretKey        = Configuration["JwtSemmetricKey"].DecodeBase64();
            var signingSecretKey = new SigningSymmetricKey(secretKey);

            services.AddSingleton <ISigningSymmetricKey>(signingSecretKey);

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultScheme             = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(cfg =>
            {
                cfg.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer = true,
                    ValidIssuer    = "RecipeBookApp",

                    ValidateAudience = true,
                    ValidAudience    = "RecipeBookClient",

                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey)),

                    ValidateLifetime = true,
                    ClockSkew        = TimeSpan.FromSeconds(5)
                };
            });


            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            services.AddResponseCaching();

            services.AddAWSService <Amazon.S3.IAmazonS3>();
        }