コード例 #1
0
        public static void ConfigureSharedServices(IConfigurationRoot configuration, IServiceCollection services)
        {
            var deployBucket = configuration.GetSection("AWS").GetSection("S3")["DeployBucket"];
            var loggingDbConnectionStringKey = configuration.GetSection("AWS").GetSection("RDS")["LoggingDbConnectionStringKey"];
            var appDbConnectionStringKey     = configuration.GetSection("AWS").GetSection("RDS")["AppDbConnectionStringKey"];

            var s3Client       = new AmazonS3Client(RegionEndpoint.USWest2);
            var storageAdapter = new AwsS3Adapter(s3Client);

            var loggingDbConnectionString = storageAdapter.GetObjectAsync(deployBucket, loggingDbConnectionStringKey).Result;
            var appDbConnectionString     = storageAdapter.GetObjectAsync(deployBucket, appDbConnectionStringKey).Result;

            services.AddNpgLoggerScoped(loggingDbConnectionString, LogLevel.All);

            services
            .AddEntityFrameworkNpgsql()
            .AddDbContext <RemindMeDatabaseContext>(options =>
                                                    options.UseNpgsql(appDbConnectionString));
            services.AddScoped <IReminderRepository, ReminderRepository>();
        }
コード例 #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public virtual void ConfigureServices(IServiceCollection services)
        {
            ConfigurationHelper.ConfigureSharedServices(Configuration, services);

            var deployBucket     = Configuration.GetSection("AWS").GetSection("S3")["DeployBucket"];
            var cognitoConfigKey = Configuration.GetSection("AWS").GetSection("S3")["CognitoConfigurationKey"];

            var s3Client       = new AmazonS3Client(RegionEndpoint.USWest2);
            var storageAdapter = new AwsS3Adapter(s3Client);

            var cognitoAdapterConfig = JsonConvert.DeserializeObject <AwsCognitoAdapterConfig>(storageAdapter.GetObjectAsync(deployBucket, cognitoConfigKey).Result);
            var validIssuer          = Configuration.GetValidIssuer(cognitoAdapterConfig.UserPoolId);

            services.Configure <CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                //options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.Events = new JwtBearerEvents
                {
                    OnMessageReceived = context => {
                        context.Token = context.Request.Cookies["IdToken"];
                        return(Task.CompletedTask);
                    }
                };

                options.TokenValidationParameters = new TokenValidationParameters
                {
                    IssuerSigningKeyResolver = (s, token, identifier, parameters) =>
                    {
                        var json = new WebClient().DownloadString($"{validIssuer}/.well-known/jwks.json");
                        var keys = JsonConvert.DeserializeObject <JsonWebKeySet>(json).Keys;
                        return((IEnumerable <SecurityKey>)keys);
                    },
                    ClockSkew         = TimeSpan.FromMinutes(5),
                    LifetimeValidator = (notBefore, expires, token, parameters) =>
                    {
                        if (expires != null)
                        {
                            //return false;
                            if (DateTime.UtcNow < expires)
                            {
                                return(true);
                            }
                        }
                        return(false);
                    },
                    ValidateIssuer           = true,
                    ValidateAudience         = false,
                    ValidateLifetime         = true,
                    ValidateIssuerSigningKey = true,
                    ValidIssuer = validIssuer
                };
            });

            services.AddScoped <AwsCognitoAdapterConfig>(s => cognitoAdapterConfig);
            services.AddScoped <IAwsCognitoAdapterHelper, AwsCognitoAdapterHelper>();
            services.AddScoped <IAmazonCognitoIdentityProvider, AmazonCognitoIdentityProviderClient>();
            services.AddScoped <IAuthAdapter, AwsCognitoAdapter>();

            services.AddScoped <IUserRepository, UserRepository>();
        }