aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Startup.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2019-04-13 13:03:18 +0800
committercrupest <crupest@outlook.com>2019-04-13 13:03:18 +0800
commit72890735ced2edc8ccecfed811393e951de5c091 (patch)
tree39cf181a18a3dc443dbab5669a04d0a23cdefd00 /Timeline/Startup.cs
parent19cae15eba2bcede41b818e1b8ab7fd5ac92eb05 (diff)
downloadtimeline-72890735ced2edc8ccecfed811393e951de5c091.tar.gz
timeline-72890735ced2edc8ccecfed811393e951de5c091.tar.bz2
timeline-72890735ced2edc8ccecfed811393e951de5c091.zip
Init separate.
Diffstat (limited to 'Timeline/Startup.cs')
-rw-r--r--Timeline/Startup.cs48
1 files changed, 26 insertions, 22 deletions
diff --git a/Timeline/Startup.cs b/Timeline/Startup.cs
index 88348892..f7b2e64d 100644
--- a/Timeline/Startup.cs
+++ b/Timeline/Startup.cs
@@ -1,7 +1,6 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
-using Microsoft.AspNetCore.SpaServices.AngularCli;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Authentication.JwtBearer;
@@ -16,11 +15,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.
@@ -31,14 +34,26 @@ 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("JwtConfig"));
- var jwtConfig = Configuration.GetSection("JwtConfig").Get<JwtConfig>();
+ services.Configure<JwtConfig>(Configuration.GetSection(nameof(JwtConfig)));
+ var jwtConfig = Configuration.GetSection(nameof(JwtConfig)).Get<JwtConfig>();
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(o =>
@@ -57,9 +72,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();
}
@@ -68,14 +83,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 =>
@@ -84,16 +98,6 @@ namespace Timeline
name: "default",
template: "{controller}/{action=Index}/{id?}");
});
-
- app.UseSpa(spa =>
- {
- spa.Options.SourcePath = "ClientApp";
-
- if (env.IsDevelopment())
- {
- spa.UseAngularCliServer(npmScript: "start");
- }
- });
}
}
}