예제 #1
0
 public MailgunEmailSender(HttpClient mailgunHttpClient,
                           MailConfigSection mailConfigSection,
                           ILogger <MailgunEmailSender> logger)
 {
     this.mailgunHttpClient = mailgunHttpClient;
     this.mailConfigSection = mailConfigSection;
     this.logger            = logger;
 }
        public static IServiceCollection AddInfractructureServicesRegistration(this IServiceCollection services, IConfiguration configuration)
        {
            MailConfigSection mailConfigSection = configuration.GetSection("mailConfigSection").Get <MailConfigSection>();

            services.AddSingleton(mailConfigSection);

            services.AddHttpClient <IEmailService, MailgunEmailService>(cfg =>
            {
                cfg.BaseAddress = new Uri("https://api.mailgun.net/");
                cfg.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                                                                                        Convert.ToBase64String(Encoding.UTF8.GetBytes($"api:{mailConfigSection.MailgunKey}")));
            });

            services.AddTransient <IFileService, FileService>();
            return(services);
        }
예제 #3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            MailConfigSection mailConfigSection = Configuration.GetSection("MailGunConfigSection").Get <MailConfigSection>();

            services.AddSingleton(mailConfigSection);

            services.AddHttpClient <IEmailSender, MailgunEmailSender>(config =>
            {
                config.BaseAddress = new Uri("https://api.mailgun.net");
                config.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                                                                                           Convert.ToBase64String(Encoding.UTF8.GetBytes($"api:{mailConfigSection.MailgunKey}")));
            });
            services.AddHostedService <NotificationService>();
            services.AddSingleton <ISubscriptionClient>(x =>
                                                        new SubscriptionClient(Configuration.GetValue <string>("ServiceBus:ConnectionString"),
                                                                               Configuration.GetValue <string>("ServiceBus:TopicName"),
                                                                               Configuration.GetValue <string>("ServiceBus:SubscriptionName")));
        }
예제 #4
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();

            services.AddHealthChecks();

            services.AddDbContext <ApplicationDbContext>(options =>
            {
                options.UseSqlServer(
                    Configuration.GetConnectionString("MaxDb"));
            });

            MailConfigSection mailConfigSection = Configuration.GetSection("mailConfigSection").Get <MailConfigSection>();

            services.AddSingleton(mailConfigSection);

            services.AddScoped <IUserServices, UserServices>();
            services.AddScoped <INotificationServices, NotificationServices>();

            services.AddSingleton <IJwtAuthManager, JwtAuthManager>();

            var jwtTokenConfig = Configuration.GetSection("jwtTokenConfig").Get <JwtTokenConfig>();

            services.AddSingleton(jwtTokenConfig);
            services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(x =>
            {
                x.RequireHttpsMetadata      = true;
                x.SaveToken                 = true;
                x.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer           = true,
                    ValidIssuer              = jwtTokenConfig.Issuer,
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtTokenConfig.Secret)),
                    ValidAudience            = jwtTokenConfig.Audience,
                    ValidateAudience         = true,
                    ValidateLifetime         = true,
                    ClockSkew = TimeSpan.FromMinutes(1)
                };
            });

            services.AddHostedService <JwtRefreshTokenCache>();

            services.AddHttpClient <IEmailService, MailgunEmailService>(cfg =>
            {
                cfg.BaseAddress = new Uri("https://api.mailgun.net/");
                cfg.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                                                                                        Convert.ToBase64String(Encoding.UTF8.GetBytes($"api:{mailConfigSection.MailgunKey}")));
            });

            services.AddSwaggerGen();

            services.AddCors(options =>
            {
                options.AddPolicy("AllowAll",
                                  builder => { builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader(); });
            });
        }
예제 #5
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
            services.AddControllers().AddNewtonsoftJson(options =>
                                                        options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);

            services.Configure <FormOptions>(o => {
                o.ValueLengthLimit         = int.MaxValue;
                o.MultipartBodyLengthLimit = int.MaxValue;
                o.MemoryBufferThreshold    = int.MaxValue;
            });

            services.AddCors(options =>
            {
                options.AddPolicy(name: MyAllowSpecificOrigins,
                                  builder =>
                {
                    builder.WithOrigins("http://localhost:3000")
                    .AllowAnyHeader()
                    .AllowAnyMethod();
                });
            });

            services.Configure <Mongosettings>(opt =>
            {
                opt.Connection   = Configuration.GetSection("MongoSettings:Connection").Value;
                opt.DatabaseName = Configuration.GetSection("MongoSettings:DatabaseName").Value;
            });

            MailConfigSection mailConfigSection = Configuration.GetSection("mailConfigSection").Get <MailConfigSection>();

            services.AddSingleton(mailConfigSection);

            services.AddSingleton <IMongoBookDBContext, MongoBookDBContext>();
            services.AddSingleton <IJwtAuthManager, JwtAuthManager>();
            services.AddHostedService <JwtRefreshTokenCache>();
            services.AddScoped <IUserService, UserService>();
            services.AddScoped <IBookRepository, BookRepository>();
            //services.AddScoped<IEmailService, MailgunEmailService>();

            var jwtTokenConfig = Configuration.GetSection("jwtTokenConfig").Get <JwtTokenConfig>();

            services.AddSingleton(jwtTokenConfig);
            services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(x =>
            {
                x.RequireHttpsMetadata      = true;
                x.SaveToken                 = true;
                x.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer           = true,
                    ValidIssuer              = jwtTokenConfig.Issuer,
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtTokenConfig.Secret)),
                    ValidAudience            = jwtTokenConfig.Audience,
                    ValidateAudience         = true,
                    ValidateLifetime         = true,
                    ClockSkew = TimeSpan.FromMinutes(1)
                };
            });

            services.AddHttpClient <IEmailService, MailgunEmailService>(cfg =>
            {
                cfg.BaseAddress = new Uri("https://api.mailgun.net/");
                cfg.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                                                                                        Convert.ToBase64String(Encoding.UTF8.GetBytes($"api:{mailConfigSection.MailgunKey}")));
            });

            services.AddCors(options =>
            {
                options.AddPolicy("AllowAll",
                                  builder => { builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader(); });
            });
        }
예제 #6
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext <RingerDbContext>(options =>
                                                    options.UseSqlServer(Configuration.GetConnectionString("RingerDbContext")));

            // security key
            string securityKey = Configuration["SecurityKey"];

            // symmmetric security key
            var symmetricSecurityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(securityKey));

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(options =>
            {
                options.RequireHttpsMetadata      = false;
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    // What to validate
                    ValidateIssuer           = true,
                    ValidateAudience         = true,
                    ValidateLifetime         = false, // validate expire time
                    ValidateIssuerSigningKey = true,

                    // setup validate data
                    ValidIssuer      = "Ringer",
                    ValidAudience    = "ringer.co.kr",
                    IssuerSigningKey = symmetricSecurityKey
                };

                // TODO: Configute the authority to the expected value for your authentication provider
                // This ensures the token is appropriately validated
                // options.Authority = "http://*****:*****@"JWT Authorization header using the Bearer scheme. \r\n\r\n 
                                Enter 'Bearer' [space] and then your token in the text input below.
                                \r\n\r\nExample: 'Bearer 12345abcdef'",
                    Name        = "Authorization",
                    In          = ParameterLocation.Header,
                    Type        = SecuritySchemeType.ApiKey,
                    Scheme      = "Bearer"
                });

                c.AddSecurityRequirement(new OpenApiSecurityRequirement()
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id   = "Bearer"
                            },
                            Scheme = "oauth2",
                            Name   = "Bearer",
                            In     = ParameterLocation.Header,
                        },
                        new List <string>()
                    }
                });
            });
        }