// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddCors(o => o.AddPolicy("MyPolicy", builder => { builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader(); })); services.Configure <ApiBehaviorOptions>(options => { options.SuppressModelStateInvalidFilter = true; options.SuppressUseValidationProblemDetailsForInvalidModelStateResponses = false; }); JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); Installer.ConfigureServices(services, Configuration); Installer.ConfigureMongoDbIdentity(services); services.AddMvc(); RegisterAuth(services); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" }); }); }
private static void ConfigureInfrastructure(IServiceCollection services) { services.AddCors(o => o.AddPolicy(CORS_POLICY, builder => { builder.WithOrigins("http://localhost:4200") .AllowAnyHeader() .AllowAnyMethod(); })); Installer.ConfigureServices(services); // Auto Mapper Configurations var mappingConfig = new MapperConfiguration(mc => { mc.AddProfile(new MappingProfile()); }); var mapper = mappingConfig.CreateMapper(); services.AddSingleton(mapper); // MVC services.AddMvc(config => { config.Filters.Add(new ModelStateCheckFilter()); }).AddJsonOptions( options => { options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; options.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc; options.SerializerSettings.DateFormatString = "yyyy-MM-dd'T'HH:mm:ssZ"; options.SerializerSettings.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Objects; options.SerializerSettings.SerializationBinder = new TypeNameSerializationBinder(); options.SerializerSettings.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore; options.SerializerSettings.MetadataPropertyHandling = Newtonsoft.Json.MetadataPropertyHandling.ReadAhead; } ); services.AddSpaStaticFiles(configuration => { configuration.RootPath = "ClientApp/dist"; }); }
public void ConfigureServices(IServiceCollection services) { services.AddAutoMapper(); services.Configure <CookiePolicyOptions>(options => { options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.None; }); services.AddDbContext <EServiceDbContext>(options => options.UseSqlServer( Configuration.GetConnectionString("DefaultConnection"), sqlServerOptions => sqlServerOptions.MigrationsAssembly("Domain.EF"))); services.AddIdentity <EServiceUser, EServiceRole>(options => { options.User.AllowedUserNameCharacters = String.Empty; options.User.RequireUniqueEmail = true; }) .AddDefaultUI(UIFramework.Bootstrap4) .AddEntityFrameworkStores <EServiceDbContext>() .AddDefaultTokenProviders(); Installer.ConfigureServices(services); services.Configure <AzureStorageConfig>(Configuration.GetSection("AzureStorageConfig")); services.AddMvc(options => options.EnableEndpointRouting = false).SetCompatibilityVersion(CompatibilityVersion.Version_2_2). AddJsonOptions(options => { options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; }); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { //connect services.AddDbContext <DataContext>(options => { options.UseSqlServer(Configuration.GetConnectionString("Main")); }); // cors services.AddCors(options => { options.AddPolicy("AllowAllHeader", builder => { builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials(); }); }); // không lấy null services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2).AddJsonOptions(options => { options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; }); //configure DAL Installer.ConfigureServices(services); }
// This method gets called by the runtime. Use th\is method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddAutoMapper(); services.AddMvc(); services.AddDbContext <PlacesContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); Installer.ConfigureServices(services); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddAutoMapper(); services.AddMvc() .AddJsonOptions(options => { options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; }); services.AddSingleton(Configuration); Installer.ConfigureServices(services, Configuration); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddAutoMapper(); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.AddDbContext <DataContext>(options => { options.UseSqlServer(Configuration.GetConnectionString("Main")); }); // Configure DAL services Installer.ConfigureServices(services); }
// This method gets called by the runtime. Use this method to add services to the container public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddAutoMapper(); services.AddMvc(); services.AddDbContext <DataContext>(options => { options.UseSqlServer(Configuration.GetConnectionString("Main")); }); //Configure DAL services Installer.ConfigureServices(services); }
public void DefaultPartitionGenerator() { var collection = new ServiceCollection(); Installer.ConfigureServices(collection); var container = collection.BuildServiceProvider(); var factory = container.GetService <IFactory <IPartitionGenerator> >(); Assert.IsNotNull(factory, "There should be a factory for this service."); var generator = factory.Create(); Assert.IsTrue(generator.GetType() == typeof(InMemoryPartitioner), "I expected the in-memory partioner to be the default..."); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddCors(); services.AddDbContext <DataContext>(options => { //options.UseSqlServer(_config.GetConnectionString("BoardGamesConnectionString")); options.UseInMemoryDatabase(databaseName: "BoardGamesDb"); }); services.AddTransient <BoardGamesSeeder>(); Installer.ConfigureServices(services); services.AddAuthentication() .AddJwtBearer(cfg => { cfg.TokenValidationParameters = new TokenValidationParameters { //ValidIssuer = _config["Tokens:Issuer"], //ValidAudience = _config["Tokens:Audience"], //IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Tokens:Key"])) ValidIssuer = "localhost", ValidAudience = "localhost", IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("this is my custom Secret key for authnetication")) }; }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.AddAutoMapper(); services.AddSwaggerGen(c => { c.AddSecurityDefinition("Bearer", new ApiKeyScheme { In = "header", Description = "Please insert JWT with Bearer into field", Name = "Authorization", Type = "apiKey" }); c.OperationFilter <SecurityRequirementsOperationFilter>(); //c.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>>() //{ // { "Bearer", new string[]{ } } //}); c.SwaggerDoc("v1", new Info { Title = "Board Games API", Version = "v1" }); var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); c.IncludeXmlComments(xmlPath); }); }
public void CreateMultiple() { var collection = new ServiceCollection(); Installer.ConfigureServices(collection); var container = collection.BuildServiceProvider(); var factory = container.GetService <IFactory <IStreamer> >(); Assert.IsNotNull(factory, "There should be a factory for this service."); var a = factory.Create(); var b = factory.Create(); Assert.IsNotNull(a, "should have created the first instance"); Assert.IsNotNull(b, "should have created a second instance"); Assert.AreNotSame(a, b, "Should have created two different instances."); Assert.IsTrue(a is FlowerPower.Streamer, "Expect that we are testing the correct class..."); }
public void ConfigureServices(IServiceCollection services) { services.AddDbContext <VacationsDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("VacationsDBConn"))); Installer.ConfigureServices(services); services.AddCors(o => o.AddPolicy("CORSPolicy", builder => { builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader(); })); services.AddIdentity <User, Role>() .AddEntityFrameworkStores <VacationsDbContext>() .AddDefaultTokenProviders(); JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(cfg => { cfg.RequireHttpsMetadata = false; cfg.SaveToken = true; cfg.TokenValidationParameters = new TokenValidationParameters { ValidateActor = false, ValidateAudience = false, ValidateLifetime = true, ValidateIssuerSigningKey = true, ValidIssuer = Configuration["Token:Issuer"], ValidAudience = Configuration["Token:Audience"], IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Token:Key"])), ClockSkew = TimeSpan.Zero }; }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.AddMvc( options => { options.OutputFormatters.Clear(); options.OutputFormatters.Add(new JsonOutputFormatter(new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, }, ArrayPool <char> .Shared)); } ).SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.AddAutoMapper(); services.AddSpaStaticFiles(configuration => { configuration.RootPath = "ClientApp/dist"; }); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { ConfigurationsManager conf = new ConfigurationsManager(); Installer.ConfigureServices(services, conf.ConnectionString); services.AddIdentity <ApplicationUser, IdentityRole <Guid> >() .AddEntityFrameworkStores <SQLContext>() .AddDefaultTokenProviders(); services.Configure <IdentityOptions>(options => { // Lockout settings options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromHours(24); options.Lockout.MaxFailedAccessAttempts = 10; options.Lockout.AllowedForNewUsers = true; // User settings options.User.RequireUniqueEmail = false; // Password settings options.Password.RequireDigit = false; options.Password.RequireNonAlphanumeric = false; options.Password.RequireLowercase = false; options.Password.RequireUppercase = false; options.Password.RequiredUniqueChars = 0; }); services.AddOptions(); services.AddScoped <IBaseRepository <ApplicationUserDtocs>, SQLRepository <ApplicationUserDtocs> >(); services.AddScoped <IBaseRepository <PersonalDiary>, SQLRepository <PersonalDiary> >(); services.AddScoped <IBaseRepository <ToDo>, SQLRepository <ToDo> >(); services.AddSingleton <IEmailSender, EmailSender>(); // configure strongly typed settings objects var appSettingsSection = Configuration.GetSection("Keys"); services.Configure <AppSettings>(appSettingsSection); // configure jwt authentication var appSettings = appSettingsSection.Get <AppSettings>(); var key = Encoding.ASCII.GetBytes(appSettings.Secret); services.AddAuthentication(x => { x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(x => { x.Events = new JwtBearerEvents { OnTokenValidated = context => { //var userService = context.HttpContext.RequestServices.GetRequiredService<ProfileManager>(); var userData = context.Principal.Identity.Name; if (userData == null) { // return unauthorized if user no longer exists context.Fail("Unauthorized"); } return(Task.CompletedTask); } }; x.RequireHttpsMetadata = false; x.SaveToken = true; x.TokenValidationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(key), ValidateIssuer = false, ValidateAudience = false }; }); //services.AddScoped<ProfileManager>(); //services.AddCors(); services.AddMvc().AddJsonOptions(options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore); services.Configure <CookiePolicyOptions>(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.MinimumSameSitePolicy = SameSiteMode.None; }); services.AddCors(options => { // BEGIN02 options.AddPolicy("AllowAllOrigins", builder => { builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod(); }); // END02 }); // backGround service // not used anymore services.AddSingleton <Microsoft.Extensions.Hosting.IHostedService, EmailTask>(); services.AddMvc(); services.AddCors(o => o.AddPolicy("CorsPolicy", builder => { builder .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials() .WithOrigins("http://localhost:4200"); })); services.AddSignalR(); services.AddSignalR(); services.AddAutoMapper(typeof(Startup)); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc().AddJsonOptions(options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore); Installer.ConfigureServices(services); }