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)
        {
            var authenticationSettings = new AuthenticationSettings();

            Configuration.GetSection("Authentication").Bind(authenticationSettings);

            services.AddSingleton(authenticationSettings);
            services.AddAuthentication(option =>
            {
                option.DefaultAuthenticateScheme = "Bearer";
                option.DefaultScheme             = "Bearer";
                option.DefaultChallengeScheme    = "Bearer";
            }).AddJwtBearer(cfg =>
            {
                cfg.RequireHttpsMetadata      = false;
                cfg.SaveToken                 = true;
                cfg.TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidIssuer   = authenticationSettings.JwtIssuer,
                    ValidAudience = authenticationSettings
                                    .JwtIssuer,
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(authenticationSettings.JwtKey))
                };
            });
            services.AddControllers().AddFluentValidation();
            services.AddDbContext <RestaurantDbContext>();
            services.AddScoped <RestaurantSeeder>();
            services.AddAutoMapper(this.GetType().Assembly);
            services.AddScoped <IRestaurantService, RestaurantService>();
            services.AddScoped <IDishService, DishService>();
            services.AddScoped <IAccountService, AccountService>();
            services.AddScoped <ErrorHandlingMiddleware>();
            services.AddScoped <IValidator <RegisterUserDto>, RegisterUserDtoValidator>();
            services.AddScoped <RequestTimeMiddleware>();
            services.AddScoped <IPasswordHasher <User>, PasswordHasher <User> >();
            services.AddSwaggerGen();
        }
Esempio n. 2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var authenticationSettings = new AuthenticationSettings();

            Configuration.GetSection("Authentication").Bind(authenticationSettings);

            services.AddSingleton(authenticationSettings);
            services.AddAuthentication(option =>
            {
                option.DefaultAuthenticateScheme = "Bearer";
                option.DefaultScheme             = "Bearer";
                option.DefaultChallengeScheme    = "Bearer";
            }).AddJwtBearer(cfg =>
            {
                cfg.RequireHttpsMetadata      = false;
                cfg.SaveToken                 = true;
                cfg.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidIssuer      = authenticationSettings.JwtIssuer,
                    ValidAudience    = authenticationSettings.JwtIssuer,
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(authenticationSettings.JwtKey)),
                };
            });

            services.AddAuthorization(options =>
            {
                options.AddPolicy("HasNationality", builder => builder.RequireClaim("Nationality", "German", "Polish"));
                options.AddPolicy("AtLeast20", builder => builder.AddRequirements(new MinimumAgeRequirement(20)));
                options.AddPolicy("CreatedAtlest2Restaurant", builder => builder.AddRequirements(new CreatedMultipleRestaurantRequirement(2)));
            });

            services.AddScoped <IAuthorizationHandler, MinimumAgeRequirementHandler>();
            services.AddScoped <IAuthorizationHandler, CreatedMultipleRestaurantRequirementHandler>();
            services.AddScoped <IAuthorizationHandler, ResourceOperationRequirementHandler>();

            services.AddControllers().AddFluentValidation();

            services.AddDbContext <RestaurantDbContext>(options =>
                                                        options.UseSqlServer(Configuration.GetConnectionString("RestaurantDbConnection")));
            services.AddScoped <RestaurantSeeder>();

            services.AddAutoMapper(this.GetType().Assembly);

            services.AddScoped <IRestaurantService, RestaurantService>();
            services.AddScoped <IDishService, DishService>();
            services.AddScoped <IAccountService, AccountService>();

            services.AddScoped <IPasswordHasher <User>, PasswordHasher <User> >();

            services.AddScoped <IValidator <RegisterUserDto>, RegisterUserDtoValidator>();
            services.AddScoped <IValidator <RestaurantQuery>, RestaurantQueryValidator>();

            services.AddScoped <ErrorHandlingMiddleware>();
            services.AddScoped <RequestTimeMiddleware>();

            services.AddScoped <IUserContextService, UserContextService>();
            services.AddHttpContextAccessor();

            services.AddSwaggerGen();
            services.AddCors(options =>
            {
                options.AddPolicy("FrontEndClient", builder =>

                                  builder.AllowAnyMethod()
                                  .AllowAnyHeader()
                                  .WithOrigins(Configuration["AllowedOrigins"])

                                  );
            });
        }
Esempio n. 3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var authenticationSettings = new AuthenticationSettings();

            Configuration.GetSection("Authentication").Bind(authenticationSettings);

            services.AddSingleton(authenticationSettings);
            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = "Bearer";
                options.DefaultScheme             = "Bearer";
                options.DefaultChallengeScheme    = "Bearer";
            }).AddJwtBearer(cfg =>
            {
                cfg.RequireHttpsMetadata      = false;
                cfg.SaveToken                 = true;
                cfg.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidIssuer      = authenticationSettings.JwtIssuer,
                    ValidAudience    = authenticationSettings.JwtIssuer,
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(authenticationSettings.JwtKey))
                };
            });
            services.AddAuthorization(options =>
            {
                options.AddPolicy("HasNationality", builder => builder.RequireClaim("Nationality"));
                options.AddPolicy("Atleast20", builder => builder.AddRequirements(new MinimumAgeRequirement(20)));
            });
            services.AddScoped <IAuthorizationHandler, MinimumAgeRequirementHandler>();
            services.AddScoped <IAuthorizationHandler, ResourceOperationRequirementHandler>();
            services.AddControllers().AddFluentValidation();
            services.AddDbContext <RestaurantDbContext>();
            services.AddScoped <RestaurantSeeder>();
            services.AddAutoMapper(this.GetType().Assembly);
            services.AddScoped <IRestaurantService, RestaurantService>();
            services.AddScoped <IDishService, DishService>();
            services.AddScoped <IAccountService, AccountService>();
            services.AddScoped <ErrorHandlingMiddleware>();
            services.AddScoped <RequestTimeMiddleware>();
            services.AddScoped <IPasswordHasher <User>, PasswordHasher <User> >();
            services.AddScoped <IValidator <RegisterUserDto>, RegisterUserDtoValidator>();
            services.AddScoped <IValidator <RestaurantQuery>, RestaurantQueryValidator>();
            services.AddScoped <IUserContextService, UserContextService>();
            services.AddHttpContextAccessor();
            services.AddSwaggerGen(options =>
            {
                options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Description =
                        "JWT Authorization header using the Bearer scheme",
                    Name   = "Authorization",
                    In     = ParameterLocation.Header,
                    Type   = SecuritySchemeType.ApiKey,
                    Scheme = "Bearer"
                });
                options.AddSecurityRequirement(new OpenApiSecurityRequirement {
                    {
                        new OpenApiSecurityScheme {
                            Reference = new OpenApiReference {
                                Id   = "Bearer", //The name of the previously defined security scheme.
                                Type = ReferenceType.SecurityScheme
                            }
                        }, new List <string>()
                    }
                });
            });
        }
Esempio n. 4
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var authenticationSettings = new AuthenticationSettings();


            Configuration.GetSection("Authentication").Bind(authenticationSettings);

            services.AddSingleton(authenticationSettings);

            services.AddAuthentication(option =>
            {
                option.DefaultAuthenticateScheme = "Bearer";
                option.DefaultScheme             = "Bearer";
                option.DefaultChallengeScheme    = "Bearer";
            }).AddJwtBearer(cfg =>
            {
                cfg.RequireHttpsMetadata      = false;
                cfg.SaveToken                 = true;
                cfg.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidIssuer      = authenticationSettings.JwtIssuer,
                    ValidAudience    = authenticationSettings.JwtIssuer,
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(authenticationSettings.JwtKey)),
                };
            }); //token
            services.AddControllers().AddFluentValidation();

            services.AddAuthorization(option =>
            {
                option.AddPolicy("HasNationality", builder =>
                                 builder.RequireClaim("Nationality")); // po Nationality mo¿na podaæ konkretna wartosc claimu np. "German, TwóStary"

                option.AddPolicy("Atleast20", builder =>
                                 builder.AddRequirements(new MinimumAgeRequirment(20)));

                option.AddPolicy("CreatedAtleast2Restaurants",
                                 builder =>
                                 builder.AddRequirements(new CreatedMultipleRestaurantsRequirment(2)));
            }); // dodawanie w³aœnej polityki o nazwie HasNationality.

            services.AddScoped <IAuthorizationHandler, MinimumAgeRequirmentHandler>();
            services.AddScoped <IAuthorizationHandler, ResourceOperationRequiremtHandler>();
            services.AddScoped <IAuthorizationHandler, CreatedMultipleRestaurantsRequirmentHandler>();

            services.AddDbContext <RestaurantDbContext>(option => option.UseSqlServer(Configuration.GetConnectionString("RestaurantDbConnection")));
            services.AddScoped <RestaurantSeeder>();

            services.AddAutoMapper(this.GetType().Assembly);
            //EfcoreRepo register
            services.AddScoped <EfCoreDishRepository>();
            services.AddScoped <EfCoreRestaurantRepository>();
            services.AddScoped <EfCoreAccountRepository>();
            //rejestracja services z inteface
            services.AddScoped <IRestaurantService, RestaurantService>();
            services.AddScoped <IDishService, DishService>();
            services.AddScoped <IAccountService, AccountService>();

            services.AddScoped <ErrorHandlingMiddleware>();
            services.AddScoped <RequestTimeMiddleware>();

            services.AddScoped <IPasswordHasher <User>, PasswordHasher <User> >();

            services.AddScoped <IValidator <RegisterUserDTO>, RegisterUserDtoValidator>();
            services.AddScoped <IValidator <RestaurantQuery>, RestaurantQueryValidator>();

            services.AddScoped <IUserContextService, UserContextService>();
            services.AddHttpContextAccessor();

            services.AddSwaggerGen(c =>
            {
                c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Name         = "Authorization",
                    Type         = SecuritySchemeType.ApiKey,
                    Scheme       = "Bearer",
                    BearerFormat = "JWT",
                    In           = ParameterLocation.Header,
                    Description  = "JWT Authorization header using the Bearer scheme."
                });
                c.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id   = "Bearer"
                            }
                        },
                        new string[] {}
                    }
                });
            });
            services.AddCors(options =>
            {
                options.AddPolicy("FrontEndClient", builder =>
                {
                    builder.AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowAnyOrigin();// WithOrigin(Configuration["AllowedOrigins"]) dla konkretnej domeny przez appsettings.json
                });
            });
        }