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.AddCustomCors(CorsOrigins.GetAllowedOrigins(Configuration.GetCorsEnvironmentName()));
            services.AddCors(options =>
            {
                options.AddPolicy(AuthorizationConstants.Cors.EnableCors, builder =>
                {
                    builder
                    //.AllowAnyOrigin()
                    .WithOrigins(CorsOrigins.GetAllowedOrigins(Configuration.GetCorsEnvironmentName()))
                    .AllowAnyHeader()
                    .AllowAnyMethod()
                    .Build();
                });
            });

            services.AddControllers();

            var jwtSettings = Configuration.GetConfiguration <JwtSettings>();

            var key = System.Text.Encoding.ASCII.GetBytes(jwtSettings.Key);

            services
            .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer           = true,
                    ValidateAudience         = true,
                    ValidateLifetime         = true,
                    ValidateIssuerSigningKey = true,

                    ValidIssuer      = jwtSettings.Issuer,
                    ValidAudience    = jwtSettings.Audience,
                    IssuerSigningKey = new SymmetricSecurityKey(key),

                    ClockSkew = TimeSpan.Zero
                };
            });

            services.AddAuthorization(auth =>
            {
                auth.AddPolicy(AuthorizationConstants.Polices.Bearer,
                               new AuthorizationPolicyBuilder()
                               .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
                               .RequireAuthenticatedUser()
                               .Build());
            });

            services.Configure <JwtSettings>(Configuration.GetSection("JwtSettings"));

            services.AddScoped <IAuthenticationService, AuthenticationService>();
        }
Esempio n. 2
0
        public WebApiSettings(IConfiguration configuration, ILambdaContext context = null) : base(configuration, context)
        {
            var settings = configuration.GetSection("Settings");

            // API SDK settings
            var lambdaFunctionName = configuration["LambdaFunctionName"] ?? settings["LambdaFunctionName"];

            if (string.IsNullOrWhiteSpace(lambdaFunctionName))
            {
                throw new Exception("Missing LambdaFunctionName in configuration");
            }
            LambdaFunctionName = lambdaFunctionName;

            // CORS
            var corsOrigin = configuration["CorsOrigin"] ?? settings["CorsOrigin"];

            if (string.IsNullOrWhiteSpace(corsOrigin))
            {
                throw new Exception("Missing CorsOrigin in configuration");
            }
            CorsOrigins.AddRange(corsOrigin.Split(','));
        }