forked from fullstackproltd/AspNetCoreSpa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartup.cs
126 lines (99 loc) · 4.3 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using AspNetCoreSpa.Server;
using AspNetCoreSpa.Server.Extensions;
using Swashbuckle.AspNetCore.Swagger;
namespace AspNetCoreSpa
{
public class Startup
{
// Order or run
//1) Constructor
//2) Configure services
//3) Configure
private IHostingEnvironment _hostingEnv;
public Startup(IHostingEnvironment env)
{
_hostingEnv = env;
Helpers.SetupSerilog();
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
if (env.IsDevelopment())
{
// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
builder.AddUserSecrets<Startup>();
}
Configuration = builder.Build();
}
public static IConfigurationRoot Configuration { get; set; }
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
if (_hostingEnv.IsDevelopment())
{
services.AddSslCertificate(_hostingEnv);
}
services.AddOptions();
services.AddResponseCompression(options =>
{
options.MimeTypes = Helpers.DefaultMimeTypes;
});
services.AddCustomDbContext();
services.AddCustomIdentity();
services.AddCustomOpenIddict();
services.AddMemoryCache();
services.RegisterCustomServices();
services.AddAntiforgery(options => options.HeaderName = "X-XSRF-TOKEN");
services.AddCustomizedMvc();
// Node services are to execute any arbitrary nodejs code from .net
services.AddNodeServices();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "AspNetCoreSpa", Version = "v1" });
});
}
public void Configure(IApplicationBuilder app)
{
app.AddDevMiddlewares();
if (_hostingEnv.IsProduction())
{
app.UseResponseCompression();
}
app.SetupMigrations();
app.UseXsrf();
app.UseStaticFiles();
app.UseIdentity();
app.UseOpenIddict();
// Add a middleware used to validate access
// tokens and protect the API endpoints.
app.UseOAuthValidation();
// Alternatively, you can also use the introspection middleware.
// Using it is recommended if your resource server is in a
// different application/separated from the authorization server.
//
// app.UseOAuthIntrospection(options => {
// options.AutomaticAuthenticate = true;
// options.AutomaticChallenge = true;
// options.Authority = "http://localhost:54895/";
// options.Audiences.Add("resource_server");
// options.ClientId = "resource_server";
// options.ClientSecret = "875sqd4s5d748z78z7ds1ff8zz8814ff88ed8ea4z4zzd";
// });
app.UseOAuthProviders();
app.UseMvc(routes =>
{
// http://stackoverflow.com/questions/25982095/using-googleoauth2authenticationoptions-got-a-redirect-uri-mismatch-error
routes.MapRoute(name: "signin-google", template: "signin-google", defaults: new { controller = "Account", action = "ExternalLoginCallback" });
routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Home", action = "Index" });
});
}
}
}