本文整理汇总了C#中IServiceCollection.ConfigureCors方法的典型用法代码示例。如果您正苦于以下问题:C# IServiceCollection.ConfigureCors方法的具体用法?C# IServiceCollection.ConfigureCors怎么用?C# IServiceCollection.ConfigureCors使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IServiceCollection
的用法示例。
在下文中一共展示了IServiceCollection.ConfigureCors方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConfigureServices
//// This method gets called by a runtime.
//// Use this method to add services to the container
//public void ConfigureServices(IServiceCollection services)
//{
// services.AddMvc();
// // Uncomment the following line to add Web API services which makes it easier to port Web API 2 controllers.
// // You will also need to add the Microsoft.AspNet.Mvc.WebApiCompatShim package to the 'dependencies' section of project.json.
// // services.AddWebApiConventions();
//}
// You probably need to change this return type - defaults to void
public IServiceProvider ConfigureServices(IServiceCollection services)
{
//IServiceProvider
// Add your various services such as MVC as normal
services.AddMvc();
services.AddCors();
services.ConfigureCors(options =>
{
options.AddPolicy("AllowAll",
builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
});
//http://docs.asp.net/en/latest/security/cors.html
//services.ConfigureCors(options =>
//{
// options.AddPolicy("AllowSpecificOrigin",
// builder => builder.WithOrigins("http://example.com"));
//});
// Use Ninject to return an instance
// Create a new Ninject kernel for your bindings
var kernel = new StandardKernel(new DataModule(Configuration["Data:DataConnection:ConnectionString"]),
new AutomapperNinjectModule(), new ServicesModule());
kernel.Populate(services);
return kernel.Get<IServiceProvider>();
}
示例2: ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services
.AddMvc()
.AddJsonOptions(options =>
options.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver()
);
services.AddCors();
services.ConfigureCors(
cors => cors.AddPolicy("AllowAll",
b => b.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()
)
);
services.Configure<MvcOptions>(
options => options.Filters.Add(
new CorsAuthorizationFilterFactory("AllowAll")
)
);
services
.AddEntityFramework()
.AddInMemoryDatabase()
.AddDbContext<TodoDbContext>(
options => options.UseInMemoryDatabase()
);
}
示例3: ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().Configure<MvcOptions>(options =>
{
var jsonFormatter = new JsonOutputFormatter
{
SerializerSettings =
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
DefaultValueHandling = DefaultValueHandling.Ignore
}
};
options.OutputFormatters.RemoveTypesOf<JsonOutputFormatter>();
options.OutputFormatters.Insert(0, jsonFormatter);
});
services.AddTransient<IContactsRepository, ContactsRepository>();
services.ConfigureCors(options =>
{
options.AddPolicy(
"CorsTutaureliaNet",
builder =>
{
builder.WithOrigins("*").AllowAnyHeader().AllowAnyMethod();
});
});
}
示例4: ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.ConfigureCors(
options =>
options.AddPolicy("allowSingleOrigin", builder => builder.WithOrigins("http://example.com")));
}
示例5: ConfigureServices
// This method gets called by a runtime.
// Use this method to add services to the container
public void ConfigureServices(IServiceCollection services)
{
services.AddInstance<IConfiguration>(Configuration);
services.AddCustomBindings(Configuration);
services.AddMvc().Configure<MvcOptions>(options =>
{
options.OutputFormatters.OfType<JsonOutputFormatter>()
.First()
.SerializerSettings
.ContractResolver = new CamelCasePropertyNamesContractResolver();
});
// CORS
services.AddCors();
var policy = new Microsoft.AspNet.Cors.Core.CorsPolicy();
policy.Headers.Add("*");
policy.Methods.Add("*");
policy.Origins.Add("*");
policy.SupportsCredentials = true;
services.ConfigureCors(x => x.AddPolicy("mypolicy", policy));
// Uncomment the following line to add Web API services which makes it easier to port Web API 2 controllers.
// You will also need to add the Microsoft.AspNet.Mvc.WebApiCompatShim package to the 'dependencies' section of project.json.
// services.AddWebApiConventions();
}
示例6: ConfigureServices
// This method gets called by a runtime.
// Use this method to add services to the container
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().ConfigureMvc(o=>o.SerializerSettings.ContractResolver=new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver());
services.AddCors();
services.ConfigureCors(o => o.AddPolicy("AllowAll", p => p.AllowAnyOrigin()));
}
示例7: ConfigureServices
// This method gets called by a runtime.
// Use this method to add services to the container
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.ConfigureCors(options =>
{
options.AddPolicy("AllowSpecificOrigins",
builder => builder.WithOrigins("http://localhost:15831")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
.Build());
});
//services.AddAuthorization(options =>
//{
// options.AddPolicy("GetTokenClaims",
// policy => policy.Requirements.Add(new Karama.Resources.Aspnet5.Controllers.GetTokenClaimsRequirement()));
//});
services.AddAuthorization(options =>
{
options.AddPolicy("GetTokenClaims",
policy => policy.RequireClaim("role", "gettokenclaims"));
});
services.AddMvc();
// Uncomment the following line to add Web API services which makes it easier to port Web API 2 controllers.
// You will also need to add the Microsoft.AspNet.Mvc.WebApiCompatShim package to the 'dependencies' section of project.json.
// services.AddWebApiConventions();
}
示例8: ConfigureServices
// This method gets called by a runtime.
// Use this method to add services to the container
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddCors();
services.ConfigureCors(o => o.AddPolicy("AllowAll", p => p.AllowAnyOrigin()));
// Uncomment the following line to add Web API services which makes it easier to port Web API 2 controllers.
// You will also need to add the Microsoft.AspNet.Mvc.WebApiCompatShim package to the 'dependencies' section of project.json.
// services.AddWebApiConventions();
}
示例9: ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddMvc();
services.ConfigureCors(o =>
{
var policy = new CorsPolicy();
policy.Origins.Add("http://localhost:5001");
o.AddPolicy("policy1", policy);
});
}
示例10: ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.ConfigureCors(options =>
{
options.AddPolicy("Cors",
builder => builder
.WithOrigins( "http://localhost:33654", "http://localhost:2000")
.WithHeaders( "accept", "content-type", "origin", "x-custom-header")
.WithMethods("GET", "POST", "PUT", "DELETE"));
});
services.AddMvc();
}
示例11: ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.ConfigureCors(options =>
{
// Define one or more CORS policies
options.AddPolicy("AllowSpecificOrigin",
builder =>
{
builder.WithOrigins("http://example.com");
});
});
services.Configure<MvcOptions>(options =>
{
options.Filters.Add(new CorsAuthorizationFilterFactory("AllowSpecificOrigin"));
});
}
示例12: ConfigureServices
// This method gets called by a runtime.
// Use this method to add services to the container
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
// Uncomment the following line to add Web API services which makes it easier to port Web API 2 controllers.
// You will also need to add the Microsoft.AspNet.Mvc.WebApiCompatShim package to the 'dependencies' section of project.json.
// services.AddWebApiConventions();
services.AddCors();
var policy = new Microsoft.AspNet.Cors.Core.CorsPolicy();
policy.Headers.Add("*");
policy.Methods.Add("*");
policy.Origins.Add("*");
policy.SupportsCredentials = true;
services.ConfigureCors(x => x.AddPolicy("mypolicy", policy));
}
示例13: ConfigureServices
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.ConfigureCors(options =>
options.AddPolicy("AllowAll", p => p.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()));
services.AddMvc();
services.AddSwagger(c =>
{
c.SwaggerGeneratorOptions.SingleApiVersion(new Info
{
Version = "v1",
Title = "Huzzah!",
Description = "An example ASP.NET 5 service",
TermsOfService = "Unintelligible legalese..."
});
});
}
示例14: ConfigureServices
// This method gets called by a runtime.
// Use this method to add services to the container
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<MrUserAccountService, MrUserAccountService>();
services.AddTransient<MrConfig, MrConfig>();
services.AddTransient<MrUserRepository, MrUserRepository>();
services.AddTransient<MrDatabase, MrDatabase>();
services.AddCors();
services.ConfigureCors(options =>
{
options.AddPolicy("AllowSpecificOrigins",
builder => builder.WithOrigins("http://localhost:15831", "http://localhost:15832")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
.Build());
});
services.AddMvc();
}
示例15: ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.Configure<AppSettings>(Configuration.GetConfigurationSection("AppSettings"));
// Add EF services to the services container
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<MusicStoreContext>(options =>
options.UseSqlServer(Configuration.Get("Data:DefaultConnection:ConnectionString")));
services.ConfigureCors(options =>
{
options.AddPolicy("CorsPolicy", builder =>
{
builder.WithOrigins("http://example.com");
});
});
// Add MVC services to the services container
services.AddMvc();
//Add all SignalR related services to IoC.
services.AddSignalR();
//Add InMemoryCache
services.AddSingleton<IMemoryCache, MemoryCache>();
// Add session related services.
services.AddCaching();
services.AddSession();
// Add the system clock service
services.AddSingleton<ISystemClock, SystemClock>();
// Configure Auth
services.Configure<AuthorizationOptions>(options =>
{
options.AddPolicy("ManageStore", new AuthorizationPolicyBuilder().RequireClaim("ManageStore", "Allowed").Build());
});
}