Example #1
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.AddDbContext <MvcWebContext>(options =>
                                                  options.UseSqlServer(Configuration.GetConnectionString("MvcWebContext")));

            services.AddAuthentication(//JwtBearerDefaults.AuthenticationScheme
                )
            .AddJwtBearer(options =>
            {
                options.RequireHttpsMetadata      = false;
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    // укзывает, будет ли валидироваться издатель при валидации токена
                    ValidateIssuer = true,
                    // строка, представляющая издателя
                    ValidIssuer = AuthOptions.ISSUER,

                    // будет ли валидироваться потребитель токена
                    ValidateAudience = true,
                    // установка потребителя токена
                    ValidAudience = AuthOptions.AUDIENCE,
                    // будет ли валидироваться время существования
                    ValidateLifetime = true,

                    // установка ключа безопасности
                    IssuerSigningKey = AuthOptions.GetSymmetricSecurityKey(),
                    // валидация ключа безопасности
                    ValidateIssuerSigningKey = true,
                };
            }

                          );

            services.AddMvc();
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //allow for mssql | mysql | pgsql | ram
            switch (Configuration["DbProvider"])
            {
            // case "pgsql":
            //  services.AddEntityFrameworkNpgsql().AddDbContext<ApplicationDbContext>(
            //      opt => opt.UseNpgsql(Configuration.GetConnectionString("DefaultConnectionPgsql"))
            //  );
            //  break;
            // case "mssql":
            //  services.AddEntityFrameworkSqlServer().AddDbContext<ApplicationDbContext>(
            //      opt => opt.UseSqlServer(Configuration.GetConnectionString("DefaultConnectionMsSql")));
            //  break;
            // case "mysql":
            //  services.AddEntityFrameworkMySQL().AddDbContext<ApplicationDbContext>(
            //      opt => opt.UseMySQL(Configuration.GetConnectionString("DefaultConnectionMysql")));
            //  break;
            case "ram":
                services.AddEntityFrameworkInMemoryDatabase().AddDbContext <ApplicationDbContext>(
                    opt => opt.UseInMemoryDatabase("dateBaseInMemory"));
                break;
            }


            services.AddIdentity <ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores <ApplicationDbContext>()
            .AddDefaultTokenProviders();

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })

            .AddJwtBearer(options =>
            {
                options.SaveToken                 = true;
                options.RequireHttpsMetadata      = false;
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    // укзывает, будет ли валидироваться издатель при валидации токена
                    ValidateIssuer = true,
                    // строка, представляющая издателя
                    ValidIssuer = AuthOptions.ISSUER,

                    // будет ли валидироваться потребитель токена
                    ValidateAudience = true,
                    // установка потребителя токена
                    ValidAudience = AuthOptions.AUDIENCE,
                    // будет ли валидироваться время существования
                    ValidateLifetime = true,

                    // установка ключа безопасности
                    IssuerSigningKey = AuthOptions.GetSymmetricSecurityKey(),
                    // валидация ключа безопасности
                    ValidateIssuerSigningKey = true,
                };
            });

            services.AddTransient <IUserService, UserHelper.UserService>();
            services.AddMemoryCache();
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Latest);
            services.AddControllers();

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "P53 Backend api", Version = "v1"
                });
                // var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                // var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                // c.IncludeXmlComments(xmlPath);
                // c.EnableAnnotations();
            });
        }