-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
119 lines (97 loc) · 3.79 KB
/
Program.cs
File metadata and controls
119 lines (97 loc) · 3.79 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
using ClearStore.Data;
using ClearStore.Graph;
using ClearStore.Security;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.EntityFrameworkCore;
using Microsoft.Graph;
using Microsoft.Identity.Web;
using Microsoft.Identity.Web.UI;
using Microsoft.Kiota.Abstractions.Authentication;
using System.Security.Claims;
using System.Text.Json.Serialization;
var builder = Microsoft.AspNetCore.Builder.WebApplication.CreateBuilder(args);
var initialScopes = builder.Configuration["MicrosoftGraph:Scopes"]?.Split(' ');
builder.Services
.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"))
.EnableTokenAcquisitionToCallDownstreamApi(initialScopes)
.AddMicrosoftGraph(builder.Configuration.GetSection("MicrosoftGraph"))
.AddInMemoryTokenCaches();
builder.Services.Configure<CookieAuthenticationOptions>(CookieAuthenticationDefaults.AuthenticationScheme, options =>
{
options.AccessDeniedPath = new PathString("/account/accessdenied");
options.ExpireTimeSpan = TimeSpan.FromMinutes(60);
options.SlidingExpiration = true;
});
builder.Services.AddHttpContextAccessor();
builder.Services.AddScoped(service =>
{
var httpContextAccessor = service.GetRequiredService<IHttpContextAccessor>();
var token = service.GetRequiredService<ITokenAcquisition>();
var authenticationProvider = new BaseBearerTokenAuthenticationProvider(new TokenProvider(token, builder.Configuration, httpContextAccessor));
var graphServiceClient = new GraphServiceClient(authenticationProvider);
return graphServiceClient;
});
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("administrators", b => b.RequireAssertion(c =>
c.User.HasClaim("groups", PermissionGroup.AppAdministrators)
));
options.AddPolicy("storeadmins", b => b.RequireAssertion(c =>
c.User.HasClaim("groups", PermissionGroup.AppStoreAdmins) ||
c.User.HasClaim("groups", PermissionGroup.AppAdministrators)
));
});
builder.Services
.AddDbContext<StoreContext>(options =>
{
options.UseSqlServer(builder.Configuration.GetConnectionString("StoreContext"));
options.EnableSensitiveDataLogging();
});
builder.Services.Configure<RouteOptions>(options =>
{
options.LowercaseQueryStrings = true;
options.LowercaseUrls = true;
});
builder.Services.Configure<FormOptions>(options =>
{
options.ValueCountLimit = int.MaxValue;
options.ValueLengthLimit = int.MaxValue;
options.MultipartBodyLengthLimit = int.MaxValue;
});
builder.Services.AddControllersWithViews(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
}).AddJsonOptions(options =>
{
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
});
builder.Services.AddRazorPages().AddMicrosoftIdentityUI();
builder.Services.AddMvc().AddViewOptions(options =>
{
options.HtmlHelperOptions.FormInputRenderMode = Microsoft.AspNetCore.Mvc.Rendering.FormInputRenderMode.AlwaysUseCurrentCulture;
options.HtmlHelperOptions.Html5DateRenderingMode = Microsoft.AspNetCore.Mvc.Rendering.Html5DateRenderingMode.CurrentCulture;
});
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapStaticAssets();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}")
.WithStaticAssets();
app.Run();