From 82d270a66b8ffca28e321f29b2eb90b2412ac9a7 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 8 May 2021 18:10:08 +0300 Subject: Implemented authorization; Replaced Role with IdentityRole; Renamed UserController to AccountController, updated links --- ExamTemplate/Web/Startup.cs | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) (limited to 'ExamTemplate/Web/Startup.cs') diff --git a/ExamTemplate/Web/Startup.cs b/ExamTemplate/Web/Startup.cs index 0754bff..00d94c0 100644 --- a/ExamTemplate/Web/Startup.cs +++ b/ExamTemplate/Web/Startup.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using ExamTemplate.Common; using ExamTemplate.Data; using ExamTemplate.Data.Models; using ExamTemplate.Services; @@ -11,9 +12,8 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -namespace Web { - public class Startup + public class Startup { public Startup(IConfiguration configuration) { @@ -41,10 +41,19 @@ namespace Web options.UseNpgsql(this.Configuration.GetConnectionString("LocalDBConnection"))); // Needed for SignInManager and UserManager - services.AddIdentity() - .AddRoles() + services.AddIdentity>(options => + { + options.SignIn.RequireConfirmedAccount = false; + + // Password settings + options.Password.RequireDigit = false; + options.Password.RequireLowercase = false; + options.Password.RequireNonAlphanumeric = false; + options.Password.RequireUppercase = false; + options.Password.RequiredLength = 3; + options.Password.RequiredUniqueChars = 0; + }).AddRoles>() .AddEntityFrameworkStores(); - } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -65,8 +74,8 @@ namespace Web app.UseRouting(); - app.UseAuthorization(); app.UseAuthentication(); + app.UseAuthorization(); app.UseEndpoints(endpoints => { @@ -85,15 +94,15 @@ namespace Web dbContext.Database.Migrate(); - var roleManager = (RoleManager)serviceScope.ServiceProvider.GetService(typeof(RoleManager)); - if (!dbContext.Roles.Any(x => x.Name == Role.UserRole)) + var roleManager = (RoleManager>)serviceScope.ServiceProvider.GetService(typeof(RoleManager>)); + if (!dbContext.Roles.Any(x => x.Name == RoleConst.User)) { - Role userRole = new() { Name = Role.UserRole }; + IdentityRole userRole = new() { Name = RoleConst.User }; roleManager.CreateAsync(userRole).Wait(); } - if (!dbContext.Roles.Any(x => x.Name == Role.AdminRole)) + if (!dbContext.Roles.Any(x => x.Name == RoleConst.Admin)) { - Role adminRole = new() { Name = Role.AdminRole }; + IdentityRole adminRole = new() { Name = RoleConst.Admin }; roleManager.CreateAsync(adminRole).Wait(); } -- cgit v1.2.3