-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathStartup.cs
More file actions
106 lines (92 loc) · 4.24 KB
/
Startup.cs
File metadata and controls
106 lines (92 loc) · 4.24 KB
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
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using HwProj.AuthService.API.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using HwProj.AuthService.API.Services;
using HwProj.EventBus.Client.Interfaces;
using Microsoft.IdentityModel.Tokens;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using HwProj.Utils.Configuration;
using HwProj.Utils.Authorization;
using Microsoft.AspNetCore.Authentication.Google;
using HwProj.Models.AuthService.ViewModels;
namespace HwProj.AuthService.API
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.ConfigureHwProjServices("AuthService API");
//var appSettingsSection = Configuration.GetSection("AppSettings");
//services.Configure<AppSettings>(appSettingsSection);
services.AddAuthentication(options =>
{
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
})
.AddJwtBearer(x =>
{
x.RequireHttpsMetadata = false; //TODO: dev env setting
x.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = "AuthService",
ValidateIssuer = true,
ValidateAudience = false,
ValidateLifetime = true,
IssuerSigningKey = AuthorizationKey.SecurityKey,
ValidateIssuerSigningKey = true
};
})
.AddCookie()
.AddGoogle(options =>
{
IConfigurationSection googleAuthNSection =
Configuration.GetSection("Authentication:Google");
options.ClientId = googleAuthNSection["ClientId"];
options.ClientSecret = googleAuthNSection["ClientSecret"];
});
var connectionString = ConnectionString.GetConnectionString(Configuration);
services.AddDbContext<IdentityContext>(options =>
options.UseSqlServer(connectionString));
services.AddIdentity<UserViewModel, IdentityRole>(opts =>
{
opts.User.RequireUniqueEmail = true;
opts.Password.RequiredLength = 6;
opts.Password.RequireNonAlphanumeric = false;
opts.Password.RequireLowercase = false;
opts.Password.RequireUppercase = false;
opts.Password.RequireDigit = false;
})
.AddEntityFrameworkStores<IdentityContext>()
.AddUserManager<UserManager<UserViewModel>>()
.AddRoleManager<RoleManager<IdentityRole>>()
.AddDefaultTokenProviders();
services.AddEventBus(Configuration);
services.AddScoped<IAuthTokenService, AuthTokenService>()
.AddScoped<IAccountService, AccountService>()
.AddScoped<IUserManager, ProxyUserManager>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.ConfigureHwProj(env, "AuthService API");
using (var scope = app.ApplicationServices.CreateScope())
{
var userManager = scope.ServiceProvider.GetService(typeof(UserManager<UserViewModel>)) as UserManager<UserViewModel>;
var rolesManager = scope.ServiceProvider.GetService(typeof(RoleManager<IdentityRole>)) as RoleManager<IdentityRole>;
var eventBus = scope.ServiceProvider.GetService<IEventBus>();
if (env.IsDevelopment())
{
RoleInitializer.InitializeAsync(userManager, rolesManager, eventBus).Wait();
}
}
}
}
}