-
Notifications
You must be signed in to change notification settings - Fork 7
/
Startup.cs
154 lines (144 loc) · 6.39 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Amazon.S3;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SpaServices.Webpack;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Primitives;
using Microsoft.IdentityModel.Tokens;
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
using VueChatApp.Data;
using VueChatApp.Features.AccessControl.Entities;
using VueChatApp.Features.Chat.Services;
using VueChatApp.Features.Documents.Buckets.Services;
using VueChatApp.Features.DocumentsManager.Documents.Services;
using VueChatApp.Features.QrCode;
using VueChatApp.Hubs;
using VueChatApp.Services.CloudStorage;
using VueChatApp.Utils;
namespace VueChatApp
{
public class Startup
{
private IHostingEnvironment _appEnv;
public Startup(IConfiguration configuration, IHostingEnvironment appEnv)
{
Configuration = configuration;
_appEnv = appEnv;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddSignalR();
services.AddScoped<VideoConverter>();
services.AddScoped<IQrCodeGeneratorService, QrCodeGeneratorService>();
services.AddAWSService<IAmazonS3>();
services.AddScoped<ICloudStorageService, S3StorageService>();
services.AddScoped<IBucketService, BucketService>();
services.AddScoped<IDocumentService, DocumentService>();
services.AddDbContext<ChatDbContext>(options =>
options.UseMySql(Configuration["DB"], // replace with your Connection String
mySqlOptions =>
{
mySqlOptions.UnicodeCharSet(CharSet.Utf8mb4);
mySqlOptions.EnableRetryOnFailure(3, TimeSpan.FromSeconds(30), new List<int>());
mySqlOptions.ServerVersion(new Version(5, 7, 17),
ServerType.MariaDb); // replace with your Server Version and Type
}
));
services.AddHttpClient<IChatService, ChatService>();
services.AddNodeServices();
services.AddIdentity<SystemUser, AppRole>(options =>
{
options.Password.RequireDigit = false;
options.Password.RequiredLength = 4;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequireLowercase = false;
options.User.RequireUniqueEmail = true;
options.SignIn.RequireConfirmedEmail = true;
}).AddEntityFrameworkStores<ChatDbContext>()
.AddDefaultTokenProviders();
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = Configuration["Jwt:Issuer"],
ValidAudience = Configuration["Jwt:Issuer"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:Key"]))
};
options.Events = new JwtBearerEvents
{
OnMessageReceived = context =>
{
if (context.Request.Path.Value.StartsWith("/signalr/notification-hub") &&
context.Request.Query.TryGetValue("token", out StringValues token)
)
{
context.Token = token;
}
return Task.CompletedTask;
},
OnAuthenticationFailed = context =>
{
var te = context.Exception;
return Task.CompletedTask;
}
};
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
;
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
{
HotModuleReplacement = true,
});
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
app.UseSignalR(routes =>
{
routes.MapHub<NotificationHub>("/signalr/notification-hub");
routes.MapHub<QrLoginHub>("/login-hub");
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new {controller = "Home", action = "Index"});
});
}
}
}