-
Notifications
You must be signed in to change notification settings - Fork 1
/
Startup.cs
182 lines (158 loc) · 6.71 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using BlazorMovieApp.Areas.Identity;
using BlazorMovieApp.Data;
using BlazorMovieApp.Services;
namespace BlazorMovieApp
{
public class Startup
{
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.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>().AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<IdentityUser>>();
services.AddSingleton<WeatherForecastService>();
//Add the Data Access Service
services.AddTransient<IMovieDbService, MovieDbService>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider serviceProvider)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
CreateUserAndRoles(serviceProvider).Wait();
}
private async Task CreateUserAndRoles(IServiceProvider serviceProvider)
{
//initializing custom roles
var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
var UserManager = serviceProvider.GetRequiredService<UserManager<IdentityUser>>();
string[] roleNames = { "Admin", "User" };
IdentityResult roleResult;
foreach (var roleName in roleNames)
{
var roleExist = await RoleManager.RoleExistsAsync(roleName);
if (!roleExist)
{
//create the roles and seed them to the database: Question 1
roleResult = await RoleManager.CreateAsync(new IdentityRole(roleName));
}
}
//Here you could create a super user who will maintain the web app
var poweruser = new IdentityUser
{
UserName = "test@hotmail.com",
Email = "test@hotmail.com",
};
//Ensure you have these values in your appsettings.json file
string userPWD = "Test@1234";
var _user = await UserManager.FindByEmailAsync("test@hotmail.com");
if (_user == null)
{
var createPowerUser = await UserManager.CreateAsync(poweruser, userPWD);
if (createPowerUser.Succeeded)
{
//here we tie the new user to the role
await UserManager.AddToRoleAsync(poweruser, "User");
}
}
var poweruser1 = new IdentityUser
{
UserName = "test1@hotmail.com",
Email = "test1@hotmail.com",
};
//Ensure you have these values in your appsettings.json file
string userPWD1 = "Test1@1234";
var _user1 = await UserManager.FindByEmailAsync("test1@hotmail.com");
if (_user1 == null)
{
var createPowerUser = await UserManager.CreateAsync(poweruser1, userPWD1);
if (createPowerUser.Succeeded)
{
//here we tie the new user to the role
await UserManager.AddToRoleAsync(poweruser1, "User");
}
}
var poweruser2 = new IdentityUser
{
UserName = "test2@hotmail.com",
Email = "test2@hotmail.com",
};
//Ensure you have these values in your appsettings.json file
string userPWD2 = "Test2@1234";
var _user2 = await UserManager.FindByEmailAsync("test2@hotmail.com");
if (_user2 == null)
{
var createPowerUser = await UserManager.CreateAsync(poweruser2, userPWD2);
if (createPowerUser.Succeeded)
{
//here we tie the new user to the role
await UserManager.AddToRoleAsync(poweruser2, "User");
}
}
var poweruser3 = new IdentityUser
{
UserName = "admin3@hotmail.com",
Email = "admin3@hotmail.com",
};
//Ensure you have these values in your appsettings.json file
string userPWD3 = "Admin3@1234";
var _user3 = await UserManager.FindByEmailAsync("admin3@hotmail.com");
if (_user3 == null)
{
var createPowerUser = await UserManager.CreateAsync(poweruser3, userPWD3);
if (createPowerUser.Succeeded)
{
//here we tie the new user to the role
await UserManager.AddToRoleAsync(poweruser3, "Admin");
}
}
}
}
}