Beispiel #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext <ApplicationDbContext>(options =>
                                                         options.UseSqlServer(
                                                             Configuration.GetConnectionString("DefaultConnection")));

            services.AddDatabaseDeveloperPageExceptionFilter();
            services.AddHttpContextAccessor();

            services.AddOptions <JwtOptions>()
            .Bind(Configuration.GetSection("Jwt"));
            services.AddOptions <SendGridOptions>()
            .Bind(Configuration.GetSection("SendGrid"));
            services.AddOptions <CookieAuthOptions>()
            .Bind(Configuration.GetSection("CookieAuth"));

            JwtOptions jwtInstance = JwtOptions.FromConfig(Configuration);

            services.AddAuthentication(auth =>
            {
                auth.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(bearer =>
            {
                bearer.SaveToken = true;
                bearer.TokenValidationParameters = jwtInstance.GetTokenValidationParams();
            }).AddScheme <SessionAuthOptions, SessionAuthHandler>(SessionAuthConstants.Scheme, options =>
            {
                options.KeyBytes   = Encoding.UTF8.GetBytes(Configuration.GetValue <string>("CookieAuth:Key"));
                options.CookieName = SessionAuthConstants.DefaultCookieName;
            });

            services.AddFluentEmail(Configuration["SendGrid:FromAddress"])
            .AddRazorRenderer()
            .AddSendGridSender(Configuration["SendGrid:ApiKey"]);

            services.AddSingleton <IAsyncEmailQueue, AsyncEmailQueue>();

            services.AddScoped <IEnsureService, EnsureService>();
            services.AddScoped <IAppUsersService, AppUsersService>();
            services.AddScoped <ISigninService, SigninService>();
            services.AddScoped <IAdminService, AdminService>();

            services.ConfigureApplicationCookie(cookie =>
            {
                cookie.LoginPath        = "/Account/Login";
                cookie.LogoutPath       = "/Account/Logout";
                cookie.AccessDeniedPath = "/Account/AccessDenied";
            });

            services.AddSwaggerGen(s =>
            {
                s.SwaggerDoc("v1", new OpenApiInfo
                {
                    Title       = "Ensure Logger API",
                    Description = "The API fot the cross-platform drink logger",
                    License     = new OpenApiLicense
                    {
                        Name = "MIT License",
                        Url  = new Uri("https://opensource.org/licenses/MIT"),
                    },
                    Contact = new OpenApiContact
                    {
                        Name  = "Aviv Naaman",
                        Email = "*****@*****.**",
                    },
                    Version = "v1"
                });
            });

            services.AddAntiforgery();

            services.AddControllersWithViews()
            .AddJsonOptions(json =>
            {
                // we don't want to map shared helper props (such as ApiResponse.IsError)
                // to save some time & data usage. This prop will be eavlauted on client if needed.
                json.JsonSerializerOptions.IgnoreReadOnlyProperties = true;
            })
#if DEBUG
            .AddRazorRuntimeCompilation()     // only when debugging!
#endif
            ;
        }