本文整理汇总了C#中IServiceCollection.AddCors方法的典型用法代码示例。如果您正苦于以下问题:C# IServiceCollection.AddCors方法的具体用法?C# IServiceCollection.AddCors怎么用?C# IServiceCollection.AddCors使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IServiceCollection
的用法示例。
在下文中一共展示了IServiceCollection.AddCors方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConfigureServices
// 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.AddMvc();
services.AddCors();
services.AddCors(options => options.AddPolicy("AllowAny", p => p.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()));
}
示例2: ConfigureServices
// 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.AddApplicationInsightsTelemetry(Configuration);
services.AddMvc();
services.AddCors();
services.AddCors(options =>
{
options.AddPolicy("AllowAll",
builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
});
}
示例3: 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();
}
示例4: ConfigureServices
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<AuthorizationDbContext>(options =>
options.UseSqlServer(this.Configuration["Data:DefaultConnection:ConnectionString"]));
// Add framework services.
services.AddMvc().AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver();
});
// Add CORS support
services.AddCors(options =>
{
options.AddPolicy("AllowAllOrigins",
builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
});
services.AddScoped<IClientsRepository, EntityFrameworkClientsRepository>();
services.AddScoped<IClientGroupsRepository, EntityFrameworkClientGroupsRepository>();
services.AddScoped<IGroupClaimsRepository, EntityFrameworkGroupClaimsRepository> ();
var usersApiUri = Configuration["Data:UsersApi:Uri"];
services.AddScoped<IClientUsersRepository, UsersApiClientUsersRepository>(p => new UsersApiClientUsersRepository(usersApiUri));
services.AddScoped<IUsersRepository, UsersApiUsersRepository>(p => new UsersApiUsersRepository(usersApiUri, p.GetService<IGroupClaimsRepository>()));
}
示例5: 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();
}
示例6: ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
options.SerializerSettings.DateFormatHandling = DateFormatHandling.IsoDateFormat;
});
services.AddCors(options => options.AddPolicy("AllowAll", p => p.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()));
services.AddSignalR((d) =>
{
d.Hubs.EnableDetailedErrors = true;
d.Hubs.EnableJavaScriptProxies = true;
});
services.AddScoped<IDivineLogger<SqlDataStore>, DivineLogger<SqlDataStore>>((s) =>
{
var appID = new Guid(Configuration["AppSettings:LoggerAppID"]);
var dbConnString = Configuration["Data:DivineConnectionstring"];
var sbConnString = Configuration["Data:ServiceBusConnection"];
return new DivineLogger<SqlDataStore>(new SqlDataStore(dbConnString, sbConnString), appID);
});
}
示例7: ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
//Add Cors support to the service
services.AddCors();
var policy = new Microsoft.AspNet.Cors.Infrastructure.CorsPolicy();
policy.Headers.Add("*");
policy.Methods.Add("*");
policy.Origins.Add("*");
policy.SupportsCredentials = true;
services.AddCors(x => x.AddPolicy("corsGlobalPolicy", policy));
services.AddMvc();
}
示例8: ConfigureServices
// 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.AddApplicationInsightsTelemetry(Configuration);
services.AddAuthorization();
services.AddOptions();
services.Configure<ConnectionOptions>(Configuration);
services.AddMvc(config =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
config.Filters.Add(new AuthorizeFilter(policy));
});
services.AddCors();
services.AddSwaggerGen();
services.AddRepositoryRegistrations();
}
示例9: ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.ConfigureCors(
options =>
options.AddPolicy("allowSingleOrigin", builder => builder.WithOrigins("http://example.com")));
}
示例10: ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
//Sql client not available on mono
var useInMemoryStore = Configuration["UseInMemoryStore"] == "true" ?
true :
_runtimeEnvironment.RuntimeType.Equals("Mono", StringComparison.OrdinalIgnoreCase);
// Add EF services to the services container
if (useInMemoryStore)
{
services.AddEntityFramework()
.AddInMemoryDatabase()
.AddDbContext<MusicStoreContext>(options =>
options.UseInMemoryDatabase());
}
else
{
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<MusicStoreContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
}
// Add Identity services to the services container
services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.Cookies.ApplicationCookie.AccessDeniedPath = new PathString("/Home/AccessDenied");
})
.AddEntityFrameworkStores<MusicStoreContext>()
.AddDefaultTokenProviders();
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy", builder =>
{
builder.WithOrigins("http://example.com");
});
});
// Add MVC services to the services container
services.AddMvc();
//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.AddAuthorization(options =>
{
options.AddPolicy("ManageStore", new AuthorizationPolicyBuilder().RequireClaim("ManageStore", "Allowed").Build());
});
}
示例11: ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
var useInMemoryStore = !_platform.IsRunningOnWindows
|| _platform.IsRunningOnMono
|| _platform.IsRunningOnNanoServer;
// Add EF services to the services container
if (useInMemoryStore)
{
services.AddDbContext<MusicStoreContext>(options =>
options.UseInMemoryDatabase());
}
else
{
services.AddDbContext<MusicStoreContext>(options =>
options.UseSqlServer(Configuration[StoreConfig.ConnectionStringKey.Replace("__",":")]));
}
// Add Identity services to the services container
services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.Cookies.ApplicationCookie.AccessDeniedPath = "/Home/AccessDenied";
})
.AddEntityFrameworkStores<MusicStoreContext>()
.AddDefaultTokenProviders();
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy", builder =>
{
builder.WithOrigins("http://example.com");
});
});
// Add MVC services to the services container
services.AddMvc();
// Add memory cache services
services.AddMemoryCache();
services.AddDistributedMemoryCache();
// Add session related services.
services.AddSession();
// Add the system clock service
services.AddSingleton<ISystemClock, SystemClock>();
// Configure Auth
services.AddAuthorization(options =>
{
options.AddPolicy(
"ManageStore",
authBuilder => {
authBuilder.RequireClaim("ManageStore", "Allowed");
});
});
}
示例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();
//}
// 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>();
}
示例13: 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()
);
}
示例14: ConfigureServices
// This method gets called by the runtime. It is used to add services to the container (DI).
public void ConfigureServices(IServiceCollection services)
{
// Helpers
services.AddScoped<ISecurityHelper, SecurityHelper>();
services.AddScoped<ILoginHelper, LoginHelper>();
// Router configuration
services.ConfigureRouting(routeOptions =>
{
routeOptions.AppendTrailingSlash = true;
routeOptions.LowercaseUrls = true;
});
// CORS
services.AddCors(options =>
options.AddPolicy("AllowAll", p => p.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()));
// Add and configure MVC
services.AddMvc().AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
options.SerializerSettings.DateFormatHandling = DateFormatHandling.IsoDateFormat;
options.SerializerSettings.Formatting = Formatting.Indented;
});
// Add and configure EntityFramework
var connection = Configuration.GetSection("Data:ClientDirectoryDb").Value;
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ClientDirectoryContext>(options => options.UseSqlServer(connection));
}
示例15: ConfigureServices
// This method gets called by the runtime. Use this method to add services to the container.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
#region Token Bearer
services.AddJwtAuthentication(Path.Combine(_env.ContentRootPath, "./Security"), "RsaKey.json", "noobs", "http://leadisjourney.fr");
#endregion
services.AddNHibernate(Configuration.GetConnectionString("type"), Configuration.GetConnectionString("DefaultConnection"));
// Add framework services.
services.AddMvc();
services.AddCors(option => option.AddPolicy("AllowAll", p =>
{
p.AllowAnyOrigin();
p.AllowCredentials();
p.AllowAnyMethod();
p.AllowAnyHeader();
}));
// Adding ioc Autofac
var containerBuilder = new ContainerBuilder();
containerBuilder.Populate(services);
containerBuilder.RegisterModule<GlobalRegistration>();
var container = containerBuilder.Build();
return container.Resolve<IServiceProvider>();
}