-
Notifications
You must be signed in to change notification settings - Fork 1
/
Startup.cs
291 lines (215 loc) · 10.6 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using FundingDashboardAPI.Models;
using FundingDashboardAPI.Repositories;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
namespace FundingDashboardAPI
{
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration config)
{
Configuration = config;
}
private void CheckSameSite(HttpContext httpContext, CookieOptions options)
{
if (options.SameSite == SameSiteMode.None)
{
var userAgent = httpContext.Request.Headers["User-Agent"].ToString();
// TODO: Use your User Agent library of choice here.
if (BrowserDetection.DisallowsSameSiteNone(userAgent))
{
options.SameSite = (SameSiteMode)(-1);
//options.SameSite = SameSiteMode.Unspecified;
}
}
}
// 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 https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
options.MinimumSameSitePolicy = SameSiteMode.Unspecified;
options.OnAppendCookie = cookieContext =>
CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
options.OnDeleteCookie = cookieContext =>
CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
});
//Appdb for CareersFramework DB
services.AddDbContext<AppDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("AppDb")));
///appdb for Identity
//services.AddDbContext<AppIdentityDbContext>(options =>
// options.UseSqlServer(Configuration.GetConnectionString("AppDb")));
//services.AddIdentity<AppIdentityUser, AppIdentityRole>().AddEntityFrameworkStores<AppIdentityDbContext>();
// Allow sign in via an OpenId Connect provider like OneLogin
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie(options =>
{
options.LoginPath = "";
options.AccessDeniedPath = "/security/AccessDenied";
})
.AddOpenIdConnect(options =>
{
options.ClientId = Configuration["oidc:clientid"];
options.ClientSecret = Configuration["oidc:clientsecret"];
//options.Authority = String.Format("https://{0}.onelogin.com/oidc", Configuration["oidc:region"]);
options.Authority = "https://cloudreach.onelogin.com/oidc/2";
options.SecurityTokenValidator = new JwtSecurityTokenHandler
{
InboundClaimTypeMap = new Dictionary<string, string>()
};
options.ResponseType = OpenIdConnectResponseType.IdToken;
options.GetClaimsFromUserInfoEndpoint = true;
//options.Scope.Add("jp_roles");
options.Scope.Add("groups");
//options.ClaimActions.MapJsonKey("role", "groups", "role");
options.TokenValidationParameters.RoleClaimType = "groups";
options.TokenValidationParameters.NameClaimType = "name";
}
);
services.AddAuthorization(options =>
{
options.AddPolicy("FundingAdmin", policy => policy.RequireClaim("groups", "FundingDashboardAdmin"));
options.AddPolicy("Cloudreach", policy => policy.RequireClaim("groups", "Default"));
//options.FallbackPolicy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
});
services.Configure<OidcOptions>(Configuration.GetSection("oidc"));
services.ConfigureApplicationCookie(opt =>
{
opt.LoginPath = "/Security/SignIn";
opt.AccessDeniedPath = "/security/AccessDenied";
});
//services.AddScoped<IEmployeeRepository, EmployeeSQLRepository>();
//services.AddScoped<ICountryRespository, CountrySQLRepository>();
services.AddScoped<IFundingRepository, FundingSqlRepository>();
services.AddRazorPages().AddRazorPagesOptions(options =>
{
//options.Conventions.AddPageRoute("/ListProf", "");
});
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseStatusCodePagesWithReExecute("/Security/Error", "?code={0}");
app.UseExceptionHandler("/Security/Error");
app.UseStatusCodePagesWithReExecute("/Error", "?code={0}");
app.UseExceptionHandler("/Error");
app.UseCookiePolicy(); // Before UseAuthentication or anything else that writes cookies.
//app.UseFileServer();
app.UseHttpsRedirection();
app.UseDefaultFiles();
app.UseAuthentication();
app.UseRouting();
app.UseAuthorization();
app.UseStaticFiles(new StaticFileOptions
{
//FileProvider = new PhysicalFileProvider(
// Path.Combine(env.ContentRootPath, "wwwsecure")),
//RequestPath = "/wwwsecure"
OnPrepareResponse = ctx =>
{
if (ctx.Context.Request.Path.StartsWithSegments("/wwwsecure"))
{
//check for an authenticated user
if (!ctx.Context.User.Identity.IsAuthenticated)
{
// we can send a 401 or do a redirect - here im opting for redirect
//// if not HTTP 401
//ctx.Context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
//ctx.Context.Response.Headers.Add("Cache-Control", "no-store");
////drop the response
//ctx.Context.Response.ContentLength = 0;
//ctx.Context.Response.Body = Stream.Null;
//send to a page that forces login
ctx.Context.Response.Redirect("/GetAuth");
}
}
}
});
// This is needed if running behind a reverse proxy
// like ngrok which is great for testing while developing
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
RequireHeaderSymmetry = false,
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapRazorPages();
});
}
public static class BrowserDetection
{
// Same as https://devblogs.microsoft.com/aspnet/upcoming-samesite-cookie-changes-in-asp-net-and-asp-net-core/
public static bool DisallowsSameSiteNone(string userAgent)
{
if (string.IsNullOrEmpty(userAgent))
{
return true;
}
// Note that these detections are a starting point. See https://www.chromium.org/updates/same-site/incompatible-clients for more detections.
// Cover all iOS based browsers here. This includes:
// - Safari on iOS 12 for iPhone, iPod Touch, iPad
// - WkWebview on iOS 12 for iPhone, iPod Touch, iPad
// - Chrome on iOS 12 for iPhone, iPod Touch, iPad
// All of which are broken by SameSite=None, because they use the iOS networking stack
if (userAgent.Contains("CPU iPhone OS 12") || userAgent.Contains("iPad; CPU OS 12"))
{
return true;
}
// Cover Mac OS X based browsers that use the Mac OS networking stack. This includes:
// - Safari on Mac OS X.
// This does not include:
// - Chrome on Mac OS X
// Because they do not use the Mac OS networking stack.
if (userAgent.Contains("Macintosh; Intel Mac OS X 10_14") &&
userAgent.Contains("Version/") && userAgent.Contains("Safari"))
{
return true;
}
// Cover Chrome 50-69, because some versions are broken by SameSite=None,
// and none in this range require it.
// Note: this covers some pre-Chromium Edge versions,
// but pre-Chromium Edge does not require SameSite=None.
if (userAgent.Contains("Chrome/5") || userAgent.Contains("Chrome/6"))
{
return true;
}
// Unreal Engine runs Chromium 59, but does not advertise as Chrome until 4.23. Treat versions of Unreal
// that don't specify their Chrome version as lacking support for SameSite=None.
if (userAgent.Contains("UnrealEngine") && !userAgent.Contains("Chrome"))
{
return true;
}
return false;
}
public static bool AllowsSameSiteNone(string userAgent)
{
return !DisallowsSameSiteNone(userAgent);
}
}
}
}