-
Notifications
You must be signed in to change notification settings - Fork 6
/
Startup.cs
223 lines (197 loc) · 9 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using System;
using System.Text;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.DependencyInjection;
using AspNet.Security.OpenIdConnect.Extensions;
using AspNet.Security.OpenIdConnect.Primitives;
using AspNet.Security.OpenIdConnect.Server;
using Swashbuckle.AspNetCore.Swagger;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Serilog;
using System.IdentityModel.Tokens.Jwt;
using AutoMapper;
using SampleApi.Models;
using SampleApi.Dtos;
using SampleApi.Repository;
using SampleApi.Options;
using SampleApi.Filters;
using SampleApi.Policies;
using SampleApi.Services;
namespace SampleApi
{
public class Startup
{
public IConfigurationRoot Configuration { get; set; }
public Startup(IHostingEnvironment env)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName.ToLower()}.json", optional: true)
.AddEnvironmentVariables();
Configuration = configuration.Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration)
.Enrich.FromLogContext()
.CreateLogger();
}
public void ConfigureServices(IServiceCollection services)
{
// Add options
services.AddOptions();
services.Configure<AppOptions>(Configuration);
// Add database Context
services.AddDbContextPool<ApplicationDbContext>(options =>
{
options.UseNpgsql(Configuration.Get<AppOptions>().ConnectionStrings.PostgreSqlProvider);
options.UseOpenIddict();
});
services.AddScoped(typeof(IRepository), typeof(EFRepository<ApplicationDbContext>));
// Add identity
services.AddIdentity<User, Role>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
// Add authorization policies
services.AddAuthorization(options =>
{
// Create a policy for each permission
Type type = typeof(PermissionClaims);
foreach (var permissionClaim in type.GetFields())
{
var permissionValue = permissionClaim.GetValue(null).ToString();
options.AddPolicy(permissionValue, policy => policy.Requirements.Add(new PermissionRequirement(permissionValue)));
}
});
services.AddScoped<IAuthorizationHandler, PermissionHandler>();
// Add MVC Core
services.AddMvcCore(
options =>
{
// Add global authorization filter
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
// Add global exception handler for production
options.Filters.Add(typeof(CustomExceptionFilterAttribute));
// Add global validation filter
options.Filters.Add(typeof(ValidateModelFilterAttribute));
// Add global tenant filter
options.Filters.Add(typeof(TenantFilterAttribute));
}
)
.AddJsonFormatters()
.AddAuthorization()
.AddDataAnnotations()
.AddCors()
.AddApiExplorer();
services.AddAutoMapper(cfg =>
{
cfg.AddProfile(new AutoMapperProfile());
});
// Configure Identity to use the same JWT claims as OpenIddict
services.Configure<IdentityOptions>(options =>
{
options.Password.RequiredLength = 8;
options.Password.RequireDigit = false;
options.Password.RequireLowercase = false;
options.Password.RequireUppercase = false;
options.Password.RequireNonAlphanumeric = false;
options.ClaimsIdentity.UserIdClaimType = OpenIdConnectConstants.Claims.Subject;
options.ClaimsIdentity.RoleClaimType = OpenIdConnectConstants.Claims.Role;
});
services.AddDistributedMemoryCache();
// Add OpenId Connect/OAuth2
var secretKey = Configuration.Get<AppOptions>().Jwt.SecretKey;
services.AddOpenIddict(options =>
{
options.AddEntityFrameworkCoreStores<ApplicationDbContext>();
options.AddMvcBinders();
options.EnableAuthorizationEndpoint("/connect/authorize")
//.EnableLogoutEndpoint("/connect/logout")
.EnableTokenEndpoint("/connect/token");
//.EnableUserinfoEndpoint("/api/userinfo");
options.AllowAuthorizationCodeFlow()
.AllowPasswordFlow()
.AllowRefreshTokenFlow();
// Make the "client_id" parameter mandatory when sending a token request.
// options.RequireClientIdentification();
options.EnableRequestCaching();
// During development, you can disable the HTTPS requirement.
options.DisableHttpsRequirement();
options.UseJsonWebTokens();
options.AddSigningKey(new SymmetricSecurityKey(Encoding.ASCII.GetBytes(secretKey)));
});
// Add authentication
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
JwtSecurityTokenHandler.DefaultOutboundClaimTypeMap.Clear();
services.AddAuthentication(options =>
{
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.Authority = Configuration.Get<AppOptions>().Jwt.Authority;
options.Audience = Configuration.Get<AppOptions>().Jwt.Audience;
options.RequireHttpsMetadata = false;
options.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = OpenIdConnectConstants.Claims.Subject,
RoleClaimType = OpenIdConnectConstants.Claims.Role,
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(secretKey)),
ValidateLifetime = true
};
});
// Add Swagger generator
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new Info { Title = "My Web API", Version = "v1" });
});
// Add Email Service
services.AddTransient<IEmailService, EmailService>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IRepository repository)
{
//loggerFactory.AddConsole(Configuration.GetSection("Logging"));
//loggerFactory.AddDebug();
loggerFactory.AddSerilog();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseAuthentication();
// Add CORS middleware
app.UseCors(builder =>
builder.WithOrigins("http://localhost", "http://app.example.com")
.AllowAnyMethod()
.AllowAnyHeader());
// Add Mvc middleware
app.UseMvc();
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS etc.), specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.RoutePrefix = "apidocs";
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My Web API");
});
// Database intializer
repository.EnsureDatabaseCreated();
//DbInitializer.Initialize(repository);
}
}
}