aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Startup.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2019-02-04 23:25:33 +0800
committercrupest <crupest@outlook.com>2019-02-04 23:25:33 +0800
commitfdc51c9e4ac225311e4e14923e6b48efbd05a6e1 (patch)
tree387fa924f0fbdd7b40a25029d5e7c3955764272b /Timeline/Startup.cs
downloadtimeline-fdc51c9e4ac225311e4e14923e6b48efbd05a6e1.tar.gz
timeline-fdc51c9e4ac225311e4e14923e6b48efbd05a6e1.tar.bz2
timeline-fdc51c9e4ac225311e4e14923e6b48efbd05a6e1.zip
Init commit.
Diffstat (limited to 'Timeline/Startup.cs')
-rw-r--r--Timeline/Startup.cs98
1 files changed, 98 insertions, 0 deletions
diff --git a/Timeline/Startup.cs b/Timeline/Startup.cs
new file mode 100644
index 00000000..6dde227f
--- /dev/null
+++ b/Timeline/Startup.cs
@@ -0,0 +1,98 @@
+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;
+using Microsoft.IdentityModel.Tokens;
+using System.Text;
+using Timeline.Configs;
+using Timeline.Services;
+
+namespace Timeline
+{
+ public class Startup
+ {
+ public Startup(IConfiguration configuration)
+ {
+ Configuration = configuration;
+ }
+
+ public IConfiguration Configuration { get; }
+
+ // This method gets called by the runtime. Use this method to add services to the container.
+ public void ConfigureServices(IServiceCollection services)
+ {
+ services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
+
+ // In production, the Angular files will be served from this directory
+ services.AddSpaStaticFiles(configuration =>
+ {
+ configuration.RootPath = "ClientApp/dist";
+ });
+
+
+ services.Configure<JwtConfig>(Configuration.GetSection("JwtConfig"));
+ var jwtConfig = Configuration.GetSection("JwtConfig").Get<JwtConfig>();
+
+ services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
+ .AddJwtBearer(o =>
+ {
+ o.TokenValidationParameters.ValidateIssuer = true;
+ o.TokenValidationParameters.ValidateAudience = true;
+ o.TokenValidationParameters.ValidateIssuerSigningKey = true;
+ o.TokenValidationParameters.ValidateLifetime = true;
+ o.TokenValidationParameters.ValidIssuer = jwtConfig.Issuer;
+ o.TokenValidationParameters.ValidAudience = jwtConfig.Audience;
+ o.TokenValidationParameters.IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtConfig.SigningKey));
+ });
+
+ services.AddSingleton<IUserService, UserService>();
+ }
+
+ // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
+ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
+ {
+ if (env.IsDevelopment())
+ {
+ app.UseDeveloperExceptionPage();
+ }
+ else
+ {
+ app.UseExceptionHandler("/Error");
+ // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
+ app.UseHsts();
+ }
+
+
+ // When not in test environment, use https redirection.
+ // Because I found some problems using https redirection in test mode.
+ if (!env.IsTest())
+ app.UseHttpsRedirection();
+
+
+ app.UseStaticFiles();
+ app.UseSpaStaticFiles();
+
+ app.UseAuthentication();
+
+ app.UseMvc(routes =>
+ {
+ routes.MapRoute(
+ name: "default",
+ template: "{controller}/{action=Index}/{id?}");
+ });
+
+ app.UseSpa(spa =>
+ {
+ spa.Options.SourcePath = "ClientApp";
+
+ if (env.IsDevelopment())
+ {
+ spa.UseAngularCliServer(npmScript: "start");
+ }
+ });
+ }
+ }
+}