我使用启动模板(https://aspnetboilerplate.com/Templates)使用ASP.NET Core 2.x,.NET Core(跨平台),SPA(Angular)和多种解决方案配置创建了一个项目。我创建了数据库并执行了必要的迁移。我现在可以通过localhost:4200使用该应用程序。但是,当我想使用mylocalip:4200从公司网络中的其他设备进行连接时;我收到CORS错误。
我没有更改模板的代码,但是我的ConfigureServices方法是
public IServiceProvider ConfigureServices(IServiceCollection services)
{
// MVC
services.AddMvc(
options => options.Filters.Add(new CorsAuthorizationFilterFactory(_defaultCorsPolicyName))
);
IdentityRegistrar.Register(services);
AuthConfigurer.Configure(services, _appConfiguration);
services.AddSignalR();
// Configure CORS for angular2 UI
services.AddCors(
options => options.AddPolicy(
_defaultCorsPolicyName,
builder => builder
.WithOrigins(
// App:CorsOrigins in appsettings.json can contain more than one address separated by comma.
_appConfiguration["App:CorsOrigins"]
.Split(",", StringSplitOptions.RemoveEmptyEntries)
.Select(o => o.RemovePostFix("/"))
.ToArray()
)
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
)
);
// Swagger - Enable this line and the related lines in Configure method to enable swagger UI
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new Info { Title = "Example API", Version = "v1" });
options.DocInclusionPredicate((docName, description) => true);
// Define the BearerAuth scheme that's in use
options.AddSecurityDefinition("bearerAuth", new ApiKeyScheme()
{
Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
Name = "Authorization",
In = "header",
Type = "apiKey"
});
});
// Configure Abp and Dependency Injection
return services.AddAbp<ExampleWebHostModule>(
// Configure Log4Net logging
options => options.IocManager.IocContainer.AddFacility<LoggingFacility>(
f => f.UseAbpLog4Net().WithConfig("log4net.config")
)
);
}
我的配置方法:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseAbp(options => { options.UseAbpRequestLocalization = false; }); // Initializes ABP framework.
app.UseCors(_defaultCorsPolicyName); // Enable CORS!
app.UseStaticFiles();
app.UseAuthentication();
app.UseAbpRequestLocalization();
app.UseSignalR(routes =>
{
routes.MapHub<AbpCommonHub>("/signalr");
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "defaultWithArea",
template: "{area}/{controller=Home}/{action=Index}/{id?}");
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
// Enable middleware to serve generated Swagger as a JSON endpoint
app.UseSwagger();
// Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint(_appConfiguration["App:ServerRootAddress"].EnsureEndsWith('/') + "swagger/v1/swagger.json", "Example API V1");
options.IndexStream = () => Assembly.GetExecutingAssembly()
.GetManifestResourceStream("Example.Web.Host.wwwroot.swagger.ui.index.html");
}); // URL: /swagger
}
根据这些方法,服务器应从appsettings.json文件中获取3个URL,并为其提供CORS。
("CorsOrigins": "http://localhost:4200,http://localhost:8080,http://localhost:8081")
appsettings.json中的“ http://localhost:4200”是属于Angular应用程序的我的客户端应用程序URL。我尝试将mylocalip:4200添加到CorsOrigins列表中,并尝试使用allowAnyOrigin()(它应允许任何计算机连接到服务器)在AddCors(...)中的withOrigins(...)方法中进行更改,这些更改不起作用,要么。
Error seen from Chrome Browser
更多相关问题:我搜索了文档,但找不到:ABP在生产模式下部署应用程序时的最佳实践是什么?有建议的方法吗?
预先感谢