-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartup.cs
More file actions
141 lines (122 loc) · 5.76 KB
/
Startup.cs
File metadata and controls
141 lines (122 loc) · 5.76 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
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
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
using System;
using System.Threading.Tasks;
using IdentityServer.Data;
using IdentityServer.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace IdentityServer
{
public class Startup
{
public IWebHostEnvironment Environment { get; }
public IConfiguration Configuration { get; }
public static IConfiguration StaticIConfiguration { get; private set; }
public Startup(IWebHostEnvironment environment, IConfiguration configuration)
{
Environment = environment;
Configuration = configuration;
StaticIConfiguration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
string connectionString;
if (Environment.IsDevelopment())
{
connectionString = Configuration.GetConnectionString("DefaultConnection");
}
else
{
connectionString = Configuration.GetConnectionString("ContainerDefaultConnection");
}
Console.WriteLine($"C O N N E C T I O N S T R I N G A U T H S E R V E R: {connectionString}");
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
// check if db exists and use Database.Migrate(): To apply any migrations, will create the database if does not already exist.
var dbContext = services.BuildServiceProvider().GetService<ApplicationDbContext>();
// check if database exist
if (!dbContext.Database.GetService<IRelationalDatabaseCreator>().Exists())
{
// Automatically perform database migrations: creates db if not already exist
dbContext.Database.Migrate();
}
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.Configure<IdentityOptions>(identityOptions =>
{
identityOptions.Password.RequiredLength = 8;
identityOptions.User.RequireUniqueEmail = true;
identityOptions.Password.RequireNonAlphanumeric = true;
});
services.AddIdentityServer(options =>
{
options.Events.RaiseErrorEvents = true;
options.Events.RaiseInformationEvents = true;
options.Events.RaiseFailureEvents = true;
options.Events.RaiseSuccessEvents = true;
})
.AddInMemoryIdentityResources(Config.Ids)
.AddInMemoryApiResources(Config.Apis)
.AddInMemoryClients(Config.Clients)
.AddAspNetIdentity<ApplicationUser>()
// not recommended for production - you need to store your key material somewhere secure
.AddDeveloperSigningCredential();
services.AddTransient<SeedUsers>();
// seed users
SeedUsers(services);
services.AddAuthentication()
.AddGoogle(options =>
{
// register your IdentityServer with Google at https://console.developers.google.com
// enable the Google+ API
// set the redirect URI to http://localhost:5000/signin-google
options.ClientId = "copy client ID from Google here";
options.ClientSecret = "copy client secret from Google here";
});
}
public void Configure(IApplicationBuilder app)
{
if (Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
app.UseStaticFiles();
app.UseRouting();
app.UseIdentityServer();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
});
}
// serviceCollection contains registered dependencies in the DI container
private void SeedUsers(IServiceCollection services)
{
// build the serviceCollection, returns IServiceProvider, which is used to resolve services
using (var serviceProvider = services.BuildServiceProvider())
{
// create a scope where all my operations will run in.
using (var scope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
// resolve the dependencies I need
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
var userManager = scope.ServiceProvider.GetService<UserManager<ApplicationUser>>();
var logger = scope.ServiceProvider.GetService<ILogger<SeedUsers>>();
new SeedUsers(context, logger, userManager).Seed().Wait();
}
}
}
}
}