From 0ab907582c561a1e9e7da7ac4585ad92297d1a0d Mon Sep 17 00:00:00 2001 From: Dimitar Todorov <36155679+thermalthrottle@users.noreply.github.com> Date: Fri, 1 Apr 2022 23:22:57 +0300 Subject: [PATCH] Fixed errors on website loading Fixed the errors that occurred on initial website load by moving the initial user creation in the DBContext and not in the ASP.NET project --- RentACar/Data/Data/RentACarDbContext.cs | 28 +++++++++++-- ...220401141638_InitialMigration.Designer.cs} | 7 +--- ....cs => 20220401141638_InitialMigration.cs} | 3 +- .../RentACarDbContextModelSnapshot.cs | 3 -- .../Identity/Pages/Account/Register.cshtml.cs | 2 +- RentACar/WebApp/Startup.cs | 39 ------------------- RentACar/WebApp/appsettings.json | 2 +- .../WebApp/bin/Debug/net5.0/appsettings.json | 2 +- 8 files changed, 30 insertions(+), 56 deletions(-) rename RentACar/Data/Migrations/{20220401132719_initial.Designer.cs => 20220401141638_InitialMigration.Designer.cs} (98%) rename RentACar/Data/Migrations/{20220401132719_initial.cs => 20220401141638_InitialMigration.cs} (99%) diff --git a/RentACar/Data/Data/RentACarDbContext.cs b/RentACar/Data/Data/RentACarDbContext.cs index 54829fe..af66b2e 100644 --- a/RentACar/Data/Data/RentACarDbContext.cs +++ b/RentACar/Data/Data/RentACarDbContext.cs @@ -10,27 +10,47 @@ namespace Data { public virtual DbSet Cars { get; set; } public virtual DbSet Rents { get; set; } + private UserManager userManager { get; set; } + private RoleManager roleManager { get; set; } public RentACarDbContext() { - + //TODO: initialize UserManager and RoleManager } public RentACarDbContext(DbContextOptions dbContextOptions) : base(dbContextOptions) { - + //TODO: initialize UserManager and RoleManager } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { - optionsBuilder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=RentACar;Integrated Security=true;"); + optionsBuilder.UseSqlServer("Server=.;Database=RentACar;Integrated Security=true;"); } } - protected override void OnModelCreating(ModelBuilder modelBuilder) + protected override async void OnModelCreating(ModelBuilder modelBuilder) { + string[] roles = { "Admin", "Employee" }; + + foreach (string role in roles) + { + if (!await roleManager.RoleExistsAsync(role)) + { + await roleManager.CreateAsync(new IdentityRole(role)); + } + } + + User initialUser = new User + { + UserName = "admin", + PasswordHash = "admin", + }; + + modelBuilder.Entity().HasData(initialUser); + await userManager.AddToRoleAsync(initialUser, roles[0]); //modelBuilder.Entity().HasData( // new User diff --git a/RentACar/Data/Migrations/20220401132719_initial.Designer.cs b/RentACar/Data/Migrations/20220401141638_InitialMigration.Designer.cs similarity index 98% rename from RentACar/Data/Migrations/20220401132719_initial.Designer.cs rename to RentACar/Data/Migrations/20220401141638_InitialMigration.Designer.cs index 4341d7a..97721c1 100644 --- a/RentACar/Data/Migrations/20220401132719_initial.Designer.cs +++ b/RentACar/Data/Migrations/20220401141638_InitialMigration.Designer.cs @@ -10,8 +10,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace Data.Migrations { [DbContext(typeof(RentACarDbContext))] - [Migration("20220401132719_initial")] - partial class initial + [Migration("20220401141638_InitialMigration")] + partial class InitialMigration { protected override void BuildTargetModel(ModelBuilder modelBuilder) { @@ -99,9 +99,6 @@ namespace Data.Migrations b.Property("FirstName") .HasColumnType("nvarchar(max)"); - b.Property("IsAdmin") - .HasColumnType("bit"); - b.Property("LastName") .HasColumnType("nvarchar(max)"); diff --git a/RentACar/Data/Migrations/20220401132719_initial.cs b/RentACar/Data/Migrations/20220401141638_InitialMigration.cs similarity index 99% rename from RentACar/Data/Migrations/20220401132719_initial.cs rename to RentACar/Data/Migrations/20220401141638_InitialMigration.cs index 4d62b07..27fc668 100644 --- a/RentACar/Data/Migrations/20220401132719_initial.cs +++ b/RentACar/Data/Migrations/20220401141638_InitialMigration.cs @@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore.Migrations; namespace Data.Migrations { - public partial class initial : Migration + public partial class InitialMigration : Migration { protected override void Up(MigrationBuilder migrationBuilder) { @@ -72,7 +72,6 @@ namespace Data.Migrations FirstName = table.Column(type: "nvarchar(max)", nullable: true), LastName = table.Column(type: "nvarchar(max)", nullable: true), PersonalNumber = table.Column(type: "nvarchar(max)", nullable: true), - IsAdmin = table.Column(type: "bit", nullable: false), UserName = table.Column(type: "nvarchar(450)", nullable: true), NormalizedUserName = table.Column(type: "nvarchar(max)", nullable: true), Email = table.Column(type: "nvarchar(max)", nullable: true), diff --git a/RentACar/Data/Migrations/RentACarDbContextModelSnapshot.cs b/RentACar/Data/Migrations/RentACarDbContextModelSnapshot.cs index b4e9cc8..4bf9e27 100644 --- a/RentACar/Data/Migrations/RentACarDbContextModelSnapshot.cs +++ b/RentACar/Data/Migrations/RentACarDbContextModelSnapshot.cs @@ -97,9 +97,6 @@ namespace Data.Migrations b.Property("FirstName") .HasColumnType("nvarchar(max)"); - b.Property("IsAdmin") - .HasColumnType("bit"); - b.Property("LastName") .HasColumnType("nvarchar(max)"); diff --git a/RentACar/WebApp/Areas/Identity/Pages/Account/Register.cshtml.cs b/RentACar/WebApp/Areas/Identity/Pages/Account/Register.cshtml.cs index 7d26258..1bb8d65 100644 --- a/RentACar/WebApp/Areas/Identity/Pages/Account/Register.cshtml.cs +++ b/RentACar/WebApp/Areas/Identity/Pages/Account/Register.cshtml.cs @@ -95,7 +95,7 @@ namespace WebApp.Areas.Identity.Pages.Account ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); if (ModelState.IsValid) { - var user = new User { Id = Guid.NewGuid().ToString(), UserName = Input.UserName, Email = Input.Email, FirstName = Input.FirstName, LastName = Input.LastName, PhoneNumber = Input.PhoneNumber, PersonalNumber = Input.EGN, IsAdmin = Input.IsAdmin}; + var user = new User { Id = Guid.NewGuid().ToString(), UserName = Input.UserName, Email = Input.Email, FirstName = Input.FirstName, LastName = Input.LastName, PhoneNumber = Input.PhoneNumber, PersonalNumber = Input.EGN}; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { diff --git a/RentACar/WebApp/Startup.cs b/RentACar/WebApp/Startup.cs index 8ed4d19..2ba6b7c 100644 --- a/RentACar/WebApp/Startup.cs +++ b/RentACar/WebApp/Startup.cs @@ -68,7 +68,6 @@ namespace API app.UseAuthentication(); app.UseAuthorization(); - CreateUserRoles(serviceProvider).Wait(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( @@ -77,43 +76,5 @@ namespace API endpoints.MapRazorPages(); }); } - - private async System.Threading.Tasks.Task CreateUserRoles(IServiceProvider serviceProvider) - { - var RoleManager = serviceProvider.GetRequiredService>(); - var UserManager = serviceProvider.GetRequiredService>(); - string[] roleNames = { "Admin", "User"}; - IdentityResult roleResult; - foreach (var roleName in roleNames) - { - var roleCheck = await RoleManager.RoleExistsAsync(roleName); - if (!roleCheck) - { - roleResult = await RoleManager.CreateAsync(new IdentityRole(roleName)); - } - } - - var user = new User(); - user.UserName = "admin"; - user.Id = Guid.NewGuid().ToString(); - user.FirstName = "Admin"; - user.LastName = "Admin"; - user.PersonalNumber = "1234567890"; - user.PhoneNumber = "0888888888"; - user.Email = "admin@admin.admin"; - user.IsAdmin = true; - string userPWD = "password"; - var _user = await UserManager.FindByNameAsync(user.UserName); - if (_user == null) - { - IdentityResult chkUser = await UserManager.CreateAsync(user, userPWD); - if (chkUser.Succeeded) - { - await UserManager.AddToRoleAsync(user, "Admin"); - } - - } - - } } } diff --git a/RentACar/WebApp/appsettings.json b/RentACar/WebApp/appsettings.json index b524ae9..5bd170a 100644 --- a/RentACar/WebApp/appsettings.json +++ b/RentACar/WebApp/appsettings.json @@ -1,6 +1,6 @@ { "ConnectionStrings": { - "DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=RentACar;Integrated Security=true;" + "DefaultConnection": "Server=.;Database=RentACar;Integrated Security=true;" }, "Logging": { "LogLevel": { diff --git a/RentACar/WebApp/bin/Debug/net5.0/appsettings.json b/RentACar/WebApp/bin/Debug/net5.0/appsettings.json index b524ae9..5bd170a 100644 --- a/RentACar/WebApp/bin/Debug/net5.0/appsettings.json +++ b/RentACar/WebApp/bin/Debug/net5.0/appsettings.json @@ -1,6 +1,6 @@ { "ConnectionStrings": { - "DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=RentACar;Integrated Security=true;" + "DefaultConnection": "Server=.;Database=RentACar;Integrated Security=true;" }, "Logging": { "LogLevel": {