-
Notifications
You must be signed in to change notification settings - Fork 42
/
Startup.cs
271 lines (247 loc) · 11.5 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using HiBlogs.EntityFramework.EntityFramework;
using Microsoft.EntityFrameworkCore;
using HiBlogs.Core.Entities;
using HiBlogs.Core.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Http;
using HiBlogs.Definitions;
using System.Reflection;
using HiBlogs.Definitions.Dependency;
using Serilog;
using Serilog.Events;
using Microsoft.Extensions.Logging;
using HiBlogs.Definitions.Config;
using HiBlogs.Application;
namespace HiBlogs.Web
{
public class Startup
{
/// <summary>
/// 连接字符串
/// </summary>
public static string connection;
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
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)
{
var assemblyWeb = Assembly.GetExecutingAssembly();
var assemblyApplication = ApplicationModule.GetAssembly();
//自动注入当前程序集中的类型
AutoInjection(services, assemblyApplication);
//自动注入Application程序集中的类型
AutoInjection(services, assemblyWeb);
// 日志配置
LogConfig();
//redis http://www.cnblogs.com/savorboard/p/5592948.html
services.AddDistributedRedisCache(o => o.Configuration = AppConfig.RedisConnection);
//session (session存放在redis)
services.AddSession();
//Identity
services.AddIdentity<User, Role>(options =>
{
options.Password = new PasswordOptions()
{
RequireNonAlphanumeric = false,
RequireUppercase = false
};
}).AddEntityFrameworkStores<HiBlogsDbContext>().AddDefaultTokenProviders();
//修改默认登录、和退出链接
//https://github.com/aspnet/Security/issues/1310
services.ConfigureApplicationCookie(identityOptionsCookies =>
{
identityOptionsCookies.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;
identityOptionsCookies.LoginPath = "/Admin/Account/Login";
//identityOptionsCookies.LogoutPath = "...";
});
//Mvc
services.AddMvc();
//数据库连接
services.AddDbContext<HiBlogsDbContext>(options => options.UseMySql(AppConfig.MySqlConnection));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//注册Serilog日志框架
loggerFactory.AddSerilog();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
app.UseSession();
RouteConfig(app);
}
#region 自定义设置
/// <summary>
/// 自动注入
/// </summary>
private void AutoInjection(IServiceCollection services, Assembly assembly)
{
var types = assembly.GetTypes();
#region ISingletonDependency
//获取标记了ISingletonDependency接口的接口
var singletonInterfaceDependency = types
.Where(t => t.GetInterfaces().Contains(typeof(ISingletonDependency)))
.SelectMany(t => t.GetInterfaces().Where(f => !f.FullName.Contains(".ISingletonDependency")))
.ToList();
//自动注入标记了 ISingletonDependency接口的 接口
foreach (var interfaceName in singletonInterfaceDependency)
{
var type = types.Where(t => t.GetInterfaces().Contains(interfaceName)).FirstOrDefault();
if (type != null)
services.AddSingleton(interfaceName, type);
}
//获取标记了ISingletonDependency接口的类
var singletonTypeDependency = types
.Where(t => t.GetInterfaces().Contains(typeof(ISingletonDependency)))
.ToList();
//自动注入标记了 ISingletonDependency接口的 类
foreach (var type in singletonTypeDependency)
{
services.AddSingleton(type, type);
}
#endregion
#region ITransientDependency
//获取标记了ITransientDependency接口的接口
var transientInterfaceDependency = types
.Where(t => t.GetInterfaces().Contains(typeof(ITransientDependency)))
.SelectMany(t => t.GetInterfaces().Where(f => !f.FullName.Contains(".ITransientDependency")))
.ToList();
//自动注入标记了 ITransientDependency接口的 接口
foreach (var interfaceName in transientInterfaceDependency)
{
var type = types.Where(t => t.GetInterfaces().Contains(interfaceName)).FirstOrDefault();
if (type != null)
services.AddTransient(interfaceName, type);
}
//获取标记了ITransientDependency接口的类
var transientTypeDependency = types
.Where(t => t.GetInterfaces().Contains(typeof(ITransientDependency)))
.ToList();
//自动注入标记了 ITransientDependency接口的 类
foreach (var type in transientTypeDependency)
{
services.AddTransient(type, type);
}
#endregion
#region IScopedDependency
//获取标记了IScopedDependency接口的接口
var scopedInterfaceDependency = types
.Where(t => t.GetInterfaces().Contains(typeof(IScopedDependency)))
.SelectMany(t => t.GetInterfaces().Where(f => !f.FullName.Contains(".IScopedDependency")))
.ToList();
//自动注入标记了 IScopedDependency接口的 接口
foreach (var interfaceName in scopedInterfaceDependency)
{
var type = types.Where(t => t.GetInterfaces().Contains(interfaceName)).FirstOrDefault();
if (type != null)
services.AddScoped(interfaceName, type);
}
//获取标记了IScopedDependency接口的类
var scopedTypeDependency = types
.Where(t => t.GetInterfaces().Contains(typeof(IScopedDependency)))
.ToList();
//自动注入标记了 IScopedDependency接口的 类
foreach (var type in scopedTypeDependency)
{
services.AddScoped(type, type);
}
#endregion
}
/// <summary>
/// 日志配置
/// </summary>
private void LogConfig()
{
//nuget导入
//Serilog.Extensions.Logging
//Serilog.Sinks.RollingFile
//Serilog.Sinks.Async
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.MinimumLevel.Debug()
.MinimumLevel.Override("System", LogEventLevel.Information)
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Debug).WriteTo.Async(
a => a.RollingFile("logs/log-{Date}-Debug.txt")
))
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Information).WriteTo.Async(
a => a.RollingFile("logs/log-{Date}-Information.txt")
))
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Warning).WriteTo.Async(
a => a.RollingFile("logs/log-{Date}-Warning.txt")
))
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Error).WriteTo.Async(
a => a.RollingFile("logs/log-{Date}-Error.txt")
))
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Fatal).WriteTo.Async(
a => a.RollingFile("logs/log-{Date}-Fatal.txt")
))
.CreateLogger();
}
private void RouteConfig(IApplicationBuilder app)
{
app.UseMvc(routes =>
{
routes.MapRoute(
name: "areaRoute",
template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapRoute(
name: "userBlog",
template: "{userName}/{blogId}.html/{controller=UserBlog}/{action=Blog}");
});
}
#endregion
#region 初始化数据库数据
/// <summary>
/// 初始化数据库数据
/// </summary>
public void InitDBData()
{
using (HiBlogsDbContext _dbContext = new HiBlogsDbContext(AppConfig.MySqlConnection))
{
if (!_dbContext.Roles.Any())
{
var roleAdmin = new Role { Name = "Administrator" };
var roleAverage = new Role { Name = "Average" };
var userAdministrator = new User() { UserName = "Administrator", Email = "123@123.com", };
var userbenny = new User() { UserName = "benny", Email = "benny@123.com", };
userAdministrator.PasswordHash = new PasswordHasher<User>().HashPassword(userAdministrator, "123qwe");
userbenny.PasswordHash = new PasswordHasher<User>().HashPassword(userbenny, "123qwe");
_dbContext.Roles.Add(roleAdmin);//添加角色
_dbContext.Roles.Add(roleAverage);//添加角色
_dbContext.Users.Add(userAdministrator);//添加用户
_dbContext.Users.Add(userbenny);//添加用户
_dbContext.SaveChanges();
//给用户添加角色
_dbContext.UserRoles.Add(new IdentityUserRole<int>() { RoleId = roleAdmin.Id, UserId = userAdministrator.Id });
_dbContext.UserRoles.Add(new IdentityUserRole<int>() { RoleId = roleAverage.Id, UserId = userbenny.Id });
_dbContext.SaveChanges();
}
}
}
#endregion
}
}