public void get_requested_api_version_should_return_null_when_header_is_nullX2C_emptyX2C_or_invalid(string header, string value) { // arrange var configuration = new HttpConfiguration(); var request = new HttpRequestMessage(); var versionReader = new QueryStringOrHeaderApiVersionReader() { HeaderNames = { "api-version", "x-ms-version" } }; configuration.AddApiVersioning(o => o.ApiVersionReader = versionReader); request.SetConfiguration(configuration); if (value != null) { request.Headers.Add(header, value); } // act var version = request.GetRequestedApiVersion(); // assert version.Should().BeNull(); request.Properties["MS_ApiVersion"].Should().BeNull(); }
// 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.AddApiVersioning(_ => { _.ReportApiVersions = true; _.DefaultApiVersion = new ApiVersion(1, 0); _.AssumeDefaultVersionWhenUnspecified = true; QueryStringOrHeaderApiVersionReader mvr = new QueryStringOrHeaderApiVersionReader("version"); mvr.HeaderNames.Add("x-api-version"); }); services.AddMvc(); }
public void get_requested_api_version_should_return_expected_value_from_header(string headerName) { // arrange var requestedVersion = new ApiVersion(1, 0); var configuration = new HttpConfiguration(); var request = new HttpRequestMessage(); var versionReader = new QueryStringOrHeaderApiVersionReader() { HeaderNames = { headerName } }; configuration.AddApiVersioning(o => o.ApiVersionReader = versionReader); request.SetConfiguration(configuration); request.Headers.Add(headerName, requestedVersion.ToString()); // act var version = request.GetRequestedApiVersion(); // assert version.Should().Be(requestedVersion); request.Properties["MS_ApiVersion"].Should().Be(requestedVersion); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddSingleton(_configuration); services.AddDbContext <CampContext>(ServiceLifetime.Scoped); services.AddScoped <ICampRepository, CampRepository>(); services.AddTransient <CampDbInitializer>(); services.AddTransient <CampIdentityInitializer>(); services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddAutoMapper(); services.AddMemoryCache(); services.AddIdentity <CampUser, IdentityRole>() .AddEntityFrameworkStores <CampContext>(); services.Configure <IdentityOptions>(config => { config.Cookies.ApplicationCookie.Events = new CookieAuthenticationEvents { OnRedirectToLogin = (ctx) => { if (ctx.Request.Path.StartsWithSegments("/api") && ctx.Response.StatusCode == 200) { ctx.Response.StatusCode = 401; } return(Task.CompletedTask); }, OnRedirectToAccessDenied = (ctx) => { if (ctx.Request.Path.StartsWithSegments("/api") && ctx.Response.StatusCode == 200) { ctx.Response.StatusCode = 403; } return(Task.CompletedTask); } }; }); services.AddApiVersioning(cfg => { cfg.DefaultApiVersion = new ApiVersion(1, 1); cfg.AssumeDefaultVersionWhenUnspecified = true; cfg.ReportApiVersions = true; var rdr = new QueryStringOrHeaderApiVersionReader("ver"); rdr.HeaderNames.Add("X-Version"); cfg.ApiVersionReader = rdr; cfg.Conventions.Controller <TalksController>() .HasApiVersion(new ApiVersion(1, 0)) .HasApiVersion(new ApiVersion(1, 1)) .HasApiVersion(new ApiVersion(2, 0)); //.Action(m => m.Post(default(string),default(int),default(TalkModel))) // .MapToApiVersion(new ApiVersion(2,0)); }); services.AddCors(cfg => { cfg.AddPolicy("Wildermuth", bldr => { bldr.AllowAnyHeader() .AllowAnyMethod() .WithOrigins("http://wildermuth.com"); }); cfg.AddPolicy("AnyGET", bldr => { bldr.AllowAnyHeader() .WithMethods("GET") .AllowAnyOrigin(); }); }); services.AddAuthorization(config => { config.AddPolicy("SuperUsers", p => p.RequireClaim("SuperUser", "True")); }); // Add framework services. services.AddMvc(opt => { if (!_env.IsProduction()) { opt.SslPort = 44388; } opt.Filters.Add(new RequireHttpsAttribute()); }) .AddJsonOptions(opt => { opt.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) { // Ajoute un service pour injecter plus tard dans le constructeur services.AddSingleton(_config); services.AddDbContext <CampContext>(ServiceLifetime.Scoped); services.AddScoped <ICampRepository, CampRepository>(); services.AddTransient <CampDbInitializer>(); services.AddTransient <CampIdentityInitializer>(); services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddAutoMapper(); services.AddMemoryCache(); services.AddIdentity <CampUser, IdentityRole>() .AddEntityFrameworkStores <CampContext>(); services.Configure <IdentityOptions>(config => { config.Cookies.ApplicationCookie.Events = new CookieAuthenticationEvents() { OnRedirectToLogin = (ctx) => { if (ctx.Request.Path.StartsWithSegments("/api") && ctx.Response.StatusCode == 200) { ctx.Response.StatusCode = 401; } return(Task.CompletedTask); }, OnRedirectToAccessDenied = (ctx) => { if (ctx.Request.Path.StartsWithSegments("/api") && ctx.Response.StatusCode == 200) { ctx.Response.StatusCode = 403; } return(Task.CompletedTask); } }; }); /*services.AddCors(cfg => * { * cfg.AddPolicy("AnyCanGET", bldr => * { * bldr.AllowAnyHeader() * .WithMethods("GET") * .AllowAnyOrigin(); * * }); * * });*/ services.AddApiVersioning(cfg => { cfg.DefaultApiVersion = new ApiVersion(1, 1); cfg.AssumeDefaultVersionWhenUnspecified = true; cfg.ReportApiVersions = true; var rdr = new QueryStringOrHeaderApiVersionReader("ver"); rdr.HeaderNames.Add("X-MyCodeCamp-Version"); cfg.ApiVersionReader = rdr; /*cfg.Conventions.Controller<TalksController>() * .HasApiVersion(new ApiVersion(1, 0)) * .HasApiVersion(new ApiVersion(1, 1)) * .HasApiVersion(new ApiVersion(2, 0)) * .Action(m => m.Post(default(string), default(int), default(TalkModel))) * .MapToApiVersion(new ApiVersion(2,0));*/ }); services.AddAuthorization(cfg => { cfg.AddPolicy("SuperUsers", p => p.RequireClaim("SuperUser", "True")); }); // Add framework services. services.AddMvc() .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) { services.AddSingleton(_config); // Usually the db context and the repository that uses the db context have the same scope. // E.g. If the db context is transient then the repo is transient too. services.AddDbContext <CampContext>(ServiceLifetime.Scoped); services.AddScoped <ICampRepository, CampRepository>(); services.AddTransient <CampDbInitializer>(); services.AddTransient <CampIdentityInitializer>(); services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); // In addition to registering AutoMapper as a service here, we must also define an // AutoMapper profile in our project that shows how one type connects to another type. services.AddAutoMapper(); services.AddMemoryCache(); services.AddIdentity <CampUser, IdentityRole>() .AddEntityFrameworkStores <CampContext>(); services.Configure <IdentityOptions>(config => { // Tell Identity what to do under circumstances/events we specify. config.Cookies.ApplicationCookie.Events = new CookieAuthenticationEvents() { //OnRedirectToLogin = (context) => //{ // if (context.Request.Path.StartsWithSegments("/api") && context.Response.StatusCode == 200) // { // context.Response.StatusCode = 401; // } // return Task.CompletedTask; //}, //OnRedirectToAccessDenied = (context) => //{ // if (context.Request.Path.StartsWithSegments("/api") && context.Response.StatusCode == 200) // { // context.Response.StatusCode = 403; // } // return Task.CompletedTask; //} }; }); services.AddApiVersioning(config => { config.DefaultApiVersion = new ApiVersion(1, 1); config.AssumeDefaultVersionWhenUnspecified = true; config.ReportApiVersions = true; var rdr = new QueryStringOrHeaderApiVersionReader("ver"); rdr.HeaderNames.Add("X-MyCodeCamp-Version"); config.ApiVersionReader = rdr; config.Conventions.Controller <TalksController>() .HasApiVersion(new ApiVersion(1, 0)) .HasApiVersion(new ApiVersion(1, 1)) .HasApiVersion(new ApiVersion(2, 0)) .Action(m => m.Post(default(string), default(int), default(TalkModel))) .MapToApiVersion(new ApiVersion(2, 0)); }); // Allows Cors to be used throughout the project. services.AddCors(config => { // Add specific policies. config.AddPolicy("ESPN", builder => { builder.AllowAnyHeader() .AllowAnyMethod() .WithOrigins("http://www.espn.com"); }); config.AddPolicy("AnyGET", builder => { builder.AllowAnyHeader() .WithMethods("GET") .AllowAnyOrigin(); }); }); // Authorize certain users. services.AddAuthorization(config => { config.AddPolicy("SuperUsers", p => p.RequireClaim("SuperUser", "True")); }); // Add framework services. services.AddMvc(options => { //if (!_env.IsProduction()) //{ // options.SslPort = 44300; //} // These global filters will be added to every controller in the project. //options.Filters.Add(new RequireHttpsAttribute()); }) .AddJsonOptions(options => { options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; }); }