-
Notifications
You must be signed in to change notification settings - Fork 53
/
ServiceCollectionExtensions.cs
140 lines (122 loc) · 5.19 KB
/
ServiceCollectionExtensions.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
namespace BlazorShop.Web.Server.Infrastructure.Extensions
{
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using BlazorShop.Services.Common;
using Data;
using Data.Contracts;
using Data.Models;
using Data.Seed;
using Filters;
using Models;
using Services;
using static Data.ModelConstants.Identity;
public static class ServiceCollectionExtensions
{
public static ApplicationSettings GetApplicationSettings(
this IServiceCollection services,
IConfiguration configuration)
{
var applicationSettingsConfiguration = configuration.GetSection(nameof(ApplicationSettings));
services.Configure<ApplicationSettings>(applicationSettingsConfiguration);
return applicationSettingsConfiguration.Get<ApplicationSettings>();
}
public static IServiceCollection AddDatabase(
this IServiceCollection services,
IConfiguration configuration)
=> services
.AddDbContext<BlazorShopDbContext>(options => options
.UseSqlServer(configuration.GetDefaultConnectionString()))
.AddTransient<IInitialData, CategoriesData>()
.AddTransient<IInitialData, ProductsData>()
.AddTransient<IInitializer, BlazorShopDbInitializer>();
public static IServiceCollection AddIdentity(this IServiceCollection services)
{
services
.AddIdentity<BlazorShopUser, BlazorShopRole>(options =>
{
options.Password.RequiredLength = MinPasswordLength;
options.Password.RequireDigit = false;
options.Password.RequireLowercase = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.User.RequireUniqueEmail = true;
})
.AddEntityFrameworkStores<BlazorShopDbContext>();
return services;
}
public static IServiceCollection AddJwtAuthentication(
this IServiceCollection services,
ApplicationSettings applicationSettings)
{
var key = Encoding.ASCII.GetBytes(applicationSettings.Secret);
services
.AddAuthentication(authentication =>
{
authentication.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
authentication.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(bearer =>
{
bearer.RequireHttpsMetadata = false;
bearer.SaveToken = true;
bearer.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = false,
ValidateAudience = false
};
});
services.AddHttpContextAccessor();
services.AddScoped<ICurrentUserService, CurrentUserService>();
return services;
}
public static IServiceCollection AddApplicationServices(this IServiceCollection services)
{
var serviceInterfaceType = typeof(IService);
var singletonServiceInterfaceType = typeof(ISingletonService);
var scopedServiceInterfaceType = typeof(IScopedService);
var types = serviceInterfaceType
.Assembly
.GetExportedTypes()
.Where(t => t.IsClass && !t.IsAbstract)
.Select(t => new
{
Service = t.GetInterface($"I{t.Name}"),
Implementation = t
})
.Where(t => t.Service != null);
foreach (var type in types)
{
if (serviceInterfaceType.IsAssignableFrom(type.Service))
{
services.AddTransient(type.Service, type.Implementation);
}
else if (singletonServiceInterfaceType.IsAssignableFrom(type.Service))
{
services.AddSingleton(type.Service, type.Implementation);
}
else if (scopedServiceInterfaceType.IsAssignableFrom(type.Service))
{
services.AddScoped(type.Service, type.Implementation);
}
}
return services;
}
public static IServiceCollection AddApiControllers(this IServiceCollection services)
{
services
.AddControllers(options => options
.Filters
.Add<ModelOrNotFoundActionFilter>());
services.AddRazorPages();
return services;
}
}
}