diff options
Diffstat (limited to 'Timeline/Startup.cs')
-rw-r--r-- | Timeline/Startup.cs | 45 |
1 files changed, 24 insertions, 21 deletions
diff --git a/Timeline/Startup.cs b/Timeline/Startup.cs index 87de7501..79c2f05a 100644 --- a/Timeline/Startup.cs +++ b/Timeline/Startup.cs @@ -3,8 +3,6 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.SpaServices.AngularCli; -using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.IdentityModel.Tokens; @@ -18,11 +16,15 @@ namespace Timeline { public class Startup { - public Startup(IConfiguration configuration) + private const string corsPolicyName = "MyPolicy"; + + public Startup(IConfiguration configuration, IHostingEnvironment environment) { + Environment = environment; Configuration = configuration; } + public IHostingEnvironment Environment { get; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. @@ -33,10 +35,22 @@ namespace Timeline options.InputFormatters.Add(new StringInputFormatter()); }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2); - // In production, the Angular files will be served from this directory - services.AddSpaStaticFiles(configuration => + services.AddCors(options => { - configuration.RootPath = "ClientApp/dist"; + if (Environment.IsProduction()) + { + options.AddPolicy(corsPolicyName, builder => + { + builder.WithOrigins("www.crupest.xyz", "crupest.xyz").AllowAnyMethod().AllowAnyHeader(); + }); + } + else + { + options.AddPolicy(corsPolicyName, builder => + { + builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader(); + }); + } }); services.Configure<JwtConfig>(Configuration.GetSection(nameof(JwtConfig))); @@ -67,9 +81,9 @@ namespace Timeline } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IHostingEnvironment env) + public void Configure(IApplicationBuilder app) { - if (env.IsDevelopment()) + if (Environment.IsDevelopment()) { app.UseDeveloperExceptionPage(); } @@ -78,14 +92,13 @@ namespace Timeline app.UseExceptionHandler("/Error"); } - app.UseStaticFiles(); - app.UseSpaStaticFiles(); - app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto }); + app.UseCors(corsPolicyName); + app.UseAuthentication(); app.UseMvc(routes => @@ -94,16 +107,6 @@ namespace Timeline name: "default", template: "{controller}/{action=Index}/{id?}"); }); - - app.UseSpa(spa => - { - spa.Options.SourcePath = "ClientApp"; - - if (env.IsDevelopment()) - { - spa.UseAngularCliServer(npmScript: "start"); - } - }); } } } |