This repository has been archived by the owner on Jun 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Startup.cs
99 lines (86 loc) · 4.1 KB
/
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using DinkToPdf;
using DinkToPdf.Contracts;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.SpaServices.AngularCli;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using AspNetCore.RouteAnalyzer;
using System.Diagnostics;
using think_agro_metrics.Data;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
namespace ThinkAgroMetrics {
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.AddDbContext<DataContext> (options =>
options.UseSqlServer (Configuration.GetConnectionString ("DefaultConnection")));
//
// Database connection string.
// Make sure to update the Password value below from "Your_password123" to your actual password.
// var connection = @"Server=127.0.0.1,1433;Database=master;Integrated Security=False;User=sa;Password=Password1;MultipleActiveResultSets=True;";
// This line uses 'UseSqlServer' in the 'options' parameter
// with the connection string defined above.
// services.AddDbContext<DataContext>(
// options => options.UseSqlServer(connection));
services.AddAuthentication (JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer (options => {
options.TokenValidationParameters = new TokenValidationParameters {
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = Configuration["Jwt:Issuer"],
ValidAudience = Configuration["Jwt:Issuer"],
IssuerSigningKey = new SymmetricSecurityKey (Encoding.UTF8.GetBytes (Configuration["Jwt:Key"])),
ClockSkew = TimeSpan.Zero
};
});
services.AddMvc ();
services.AddRouteAnalyzer ();
// In production, the Angular files will be served from this directory
services.AddSpaStaticFiles (configuration => {
configuration.RootPath = "ClientApp/dist";
});
services.AddSingleton (typeof (IConverter), new SynchronizedConverter (new PdfTools ()));
}
// 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 ("/");
}
app.UseStaticFiles ();
app.UseSpaStaticFiles ();
app.UseAuthentication();
app.UseMvc (routes => {
routes.MapRouteAnalyzer ("/routes");
routes.MapRoute (
name: "default",
template: "{controller}/{action=Index}/{id?}");
});
app.UseSpa (spa => {
// To learn more about options for serving an Angular SPA from ASP.NET Core,
// see https://go.microsoft.com/fwlink/?linkid=864501
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment ()) {
spa.UseAngularCliServer (npmScript: "start");
}
});
}
}
}