This repository was archived by the owner on Mar 31, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathStartup.cs
More file actions
148 lines (127 loc) · 5.38 KB
/
Startup.cs
File metadata and controls
148 lines (127 loc) · 5.38 KB
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.Identity.Web;
using Microsoft.Identity.Web.UI;
using NuGet.Insights.Worker;
namespace NuGet.Insights.Website
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.Configure<NuGetInsightsSettings>(Configuration.GetSection(NuGetInsightsSettings.DefaultSectionName));
services.Configure<NuGetInsightsWorkerSettings>(Configuration.GetSection(NuGetInsightsSettings.DefaultSectionName));
services.Configure<NuGetInsightsWebsiteSettings>(Configuration.GetSection(NuGetInsightsSettings.DefaultSectionName));
services.AddNuGetInsights(Configuration, "NuGet.Insights.Website");
services.AddNuGetInsightsWorker();
services.AddNuGetInsightsWebsite();
services.AddApplicationInsightsTelemetry(options =>
{
options.ConnectionString = Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"];
options.EnableAdaptiveSampling = false;
});
services.AddApplicationInsightsTelemetryProcessor<RemoveInProcDependencyEvents>();
services
.AddMvc();
var microsoftIdentityBuilder = services
.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(options =>
{
Configuration.GetSection(Constants.AzureAd).Bind(options);
options.Events.OnTokenValidated = async context =>
{
await context
.HttpContext
.RequestServices
.GetRequiredService<AllowListAuthorizationHandler>()
.AddAllowedGroupClaimsAsync(context);
};
},
options =>
{
options.ExpireTimeSpan = TimeSpan.FromHours(6);
options.SlidingExpiration = true;
options.AccessDeniedPath = "/Home/AccessDenied";
});
var initialSettings = Configuration
.GetSection(NuGetInsightsSettings.DefaultSectionName)
.Get<NuGetInsightsWebsiteSettings>();
if (initialSettings?.AllowedGroups is not null && initialSettings.AllowedGroups.Any())
{
microsoftIdentityBuilder
.EnableTokenAcquisitionToCallDownstreamApi(new[] { "user.read" })
.AddInMemoryTokenCaches()
.AddMicrosoftGraph();
}
services
.AddAuthorization(options =>
{
options.AddPolicy(
AllowListAuthorizationHandler.PolicyName,
policy => policy.Requirements.Add(new AllowListRequirement()));
});
services
.AddRazorPages()
.AddMicrosoftIdentityUI();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseHsts();
app.Use(async (context, next) =>
{
context.Response.Headers["X-Content-Type-Options"] = "nosniff";
await next();
});
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(routes =>
{
routes.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}");
});
}
private class RemoveInProcDependencyEvents : ITelemetryProcessor
{
private readonly ITelemetryProcessor _next;
public RemoveInProcDependencyEvents(ITelemetryProcessor next)
{
_next = next;
}
/// <summary>
/// These are produced by the Azure SDK integration in the Application Insights client.
/// We don't want to totally disable the telemetry with <code>DependencyTrackingTelemetryModule.EnableAzureSdkTelemetryListener</code>
/// but we do want to reduce the verbosity.
/// </summary>
public void Process(ITelemetry item)
{
if (item is DependencyTelemetry dependency
&& dependency.Type != null
&& dependency.Type.StartsWith("InProc | ", StringComparison.Ordinal))
{
return;
}
_next.Process(item);
}
}
}
}