-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathProgram.cs
162 lines (138 loc) · 5.83 KB
/
Program.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
using Auth0.AspNetCore.Authentication;
using Headway.BlazorServerApp.Extensions;
using Headway.Core.Cache;
using Headway.Core.Constants;
using Headway.Core.Interface;
using Headway.Core.Model;
using Headway.Core.Notifications;
using Headway.Razor.Controls.Services;
using Headway.RemediatR.Core.Model;
using Headway.RequestApi.Api;
using Headway.RequestApi.Requests;
using MediatR;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OAuth.Claims;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;
using MudBlazor.Services;
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Net.Http;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddMediatR(typeof(ModuleApiRequest).Assembly);
builder.Services.AddMudServices();
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
var identityProvider = builder.Configuration["IdentityProvider:DefaultProvider"];
if (identityProvider.Equals(IdentityProvider.IDENTITY_SERVER_4))
{
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
{
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.Authority = builder.Configuration[$"{identityProvider}:Authority"];
options.ClientId = builder.Configuration[$"{identityProvider}:ClientId"];
options.ClientSecret = builder.Configuration[$"{identityProvider}:ClientSecret"];
options.ResponseType = "code";
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("email");
options.Scope.Add("webapi");
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
options.ClaimActions.Add(new JsonKeyClaimAction("role", "role", "role"));
options.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name",
RoleClaimType = "role"
};
});
}
else if(identityProvider.Equals(IdentityProvider.AUTH_0))
{
builder.Services
.AddAuth0WebAppAuthentication(Auth0Constants.AuthenticationScheme, options =>
{
options.Domain = builder.Configuration[$"{identityProvider}:Domain"];
options.ClientId = builder.Configuration[$"{identityProvider}:ClientId"];
options.ClientSecret = builder.Configuration[$"{identityProvider}:ClientSecret"];
options.ResponseType = "code";
}).WithAccessToken(options =>
{
options.Audience = builder.Configuration[$"{identityProvider}:Audience"];
});
}
builder.Services.AddHttpClient("webapi", client =>
{
client.BaseAddress = new Uri("https://localhost:44320");
});
builder.Services.AddScoped<TokenProvider>();
builder.Services.AddSingleton<IAppCache, AppCache>();
builder.Services.AddSingleton<IConfigCache, ConfigCache>();
builder.Services.AddSingleton<IStateNotification, StateNotification>();
builder.Services.AddTransient<IShowDialogService, ShowDialogService>();
builder.Services.AddTransient<ModulesGetRequestHandler>();
builder.Services.AddTransient<ConfigGetByNameRequestHandler>();
builder.Services.AddTransient<OptionItemsRequestHandler>();
builder.Services.AddTransient<IModuleApiRequest, ModuleApiRequest>(sp =>
{
var tokenProvider = sp.GetRequiredService<TokenProvider>();
var httpClientFactory = sp.GetRequiredService<IHttpClientFactory>();
var httpClient = httpClientFactory.CreateClient("webapi");
return new ModuleApiRequest(httpClient, tokenProvider);
});
builder.Services.AddTransient<IConfigurationApiRequest, ConfigurationApiRequest>(sp =>
{
var configCache = sp.GetRequiredService<IConfigCache>();
var tokenProvider = sp.GetRequiredService<TokenProvider>();
var httpClientFactory = sp.GetRequiredService<IHttpClientFactory>();
var httpClient = httpClientFactory.CreateClient("webapi");
return new ConfigurationApiRequest(httpClient, tokenProvider, configCache);
});
builder.Services.AddTransient<IDynamicApiRequest, DynamicApiRequest>(sp =>
{
var configCache = sp.GetRequiredService<IConfigCache>();
var tokenProvider = sp.GetRequiredService<TokenProvider>();
var httpClientFactory = sp.GetRequiredService<IHttpClientFactory>();
var httpClient = httpClientFactory.CreateClient("webapi");
return new DynamicApiRequest(httpClient, tokenProvider, configCache);
});
builder.Services.AddTransient<IOptionsApiRequest, OptionsApiRequest>(sp =>
{
var tokenProvider = sp.GetRequiredService<TokenProvider>();
var httpClientFactory = sp.GetRequiredService<IHttpClientFactory>();
var httpClient = httpClientFactory.CreateClient("webapi");
return new OptionsApiRequest(httpClient, tokenProvider);
});
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseAdditionalAssemblies(new[] { typeof(Redress).Assembly });
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
app.Run();