-
Notifications
You must be signed in to change notification settings - Fork 12
/
Program.cs
176 lines (135 loc) · 4.82 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using Azure.Identity;
using Azure.Storage.Blobs;
using Catalog.Application;
using Catalog.Application;
using Catalog.Infrastructure;
using Catalog.Infrastructure.Persistence;
using Catalog.WebApi;
using Catalog.WebApi.Hubs;
using MassTransit;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Azure;
using Microsoft.IdentityModel.Logging;
using Microsoft.IdentityModel.Tokens;
using NSwag;
using NSwag.Generation.Processors.Security;
var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;
var Configuration = builder.Configuration;
services.AddApplication(Configuration);
services.AddInfrastructure(Configuration);
services.AddServices();
services
.AddControllers()
.AddNewtonsoftJson();
services.AddHttpContextAccessor();
services.AddEndpointsApiExplorer();
// Register the Swagger services
services.AddOpenApiDocument(document =>
{
document.Title = "Web API";
document.Version = "v1";
document.AddSecurity("JWT", new OpenApiSecurityScheme
{
Type = OpenApiSecuritySchemeType.ApiKey,
Name = "Authorization",
In = OpenApiSecurityApiKeyLocation.Header,
Description = "Type into the textbox: Bearer {your JWT token}."
});
document.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("JWT"));
});
services.AddAzureClients(builder =>
{
// Add a KeyVault client
//builder.AddSecretClient(keyVaultUrl);
// Add a Storage account client
builder.AddBlobServiceClient(Configuration.GetConnectionString("Azure:Storage"))
.WithVersion(BlobClientOptions.ServiceVersion.V2019_07_07);
// Use DefaultAzureCredential by default
builder.UseCredential(new DefaultAzureCredential());
});
services.AddSignalR();
services.AddSingleton<IUserIdProvider, EmailBasedUserIdProvider>();
services.AddMassTransit(x =>
{
x.SetKebabCaseEndpointNameFormatter();
x.AddConsumers(typeof(Program).Assembly);
x.UsingRabbitMq((context, cfg) =>
{
cfg.ConfigureEndpoints(context);
});
})
.AddMassTransitHostedService()
.AddGenericRequestClient();
services.AddStackExchangeRedisCache(o =>
{
o.Configuration = Configuration.GetConnectionString("redis");
});
#if DEBUG
IdentityModelEventSource.ShowPII = true;
#endif
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
{
options.Authority = "https://identity.local";
options.Audience = "myapi";
options.TokenValidationParameters = new TokenValidationParameters()
{
NameClaimType = "name",
RoleClaimType = "role"
};
options.Events = new JwtBearerEvents
{
OnTokenValidated = context =>
{
// Add the access_token as a claim, as we may actually need it
var accessToken = context.SecurityToken as JwtSecurityToken;
if (accessToken != null)
{
ClaimsIdentity? identity = context.Principal.Identity as ClaimsIdentity;
if (identity != null)
{
identity.AddClaim(new Claim("access_token", accessToken.RawData));
}
}
return Task.CompletedTask;
}
};
//options.TokenValidationParameters.ValidateAudience = false;
//options.Audience = "openid";
//options.TokenValidationParameters.ValidTypes = new[] { "at+jwt" };
});
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseOpenApi();
app.UseSwaggerUi3(c =>
{
c.DocumentTitle = "Web API v1";
});
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapApplicationRequests();
app.MapGet("/info", () =>
{
return System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture.ToString();
})
.WithDisplayName("GetInfo")
.WithName("GetInfo")
.WithTags("Info")
.Produces<string>();
//await app.Services.SeedAsync();
app.MapControllers();
app.MapHub<ItemsHub>("/hubs/items");
app.MapHub<SomethingHub>("/hubs/something");
app.MapHub<WorkerHub>("/hubs/worker");
app.MapHub<NotificationHub>("/hubs/notifications");
app.Run();