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
This commit is contained in:
@@ -10,27 +10,47 @@ namespace Data
|
|||||||
{
|
{
|
||||||
public virtual DbSet<Car> Cars { get; set; }
|
public virtual DbSet<Car> Cars { get; set; }
|
||||||
public virtual DbSet<Rents> Rents { get; set; }
|
public virtual DbSet<Rents> Rents { get; set; }
|
||||||
|
private UserManager<User> userManager { get; set; }
|
||||||
|
private RoleManager<IdentityRole> roleManager { get; set; }
|
||||||
|
|
||||||
public RentACarDbContext()
|
public RentACarDbContext()
|
||||||
{
|
{
|
||||||
|
//TODO: initialize UserManager and RoleManager
|
||||||
}
|
}
|
||||||
|
|
||||||
public RentACarDbContext(DbContextOptions<RentACarDbContext> dbContextOptions) : base(dbContextOptions)
|
public RentACarDbContext(DbContextOptions<RentACarDbContext> dbContextOptions) : base(dbContextOptions)
|
||||||
{
|
{
|
||||||
|
//TODO: initialize UserManager and RoleManager
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||||
{
|
{
|
||||||
if (!optionsBuilder.IsConfigured)
|
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<User>().HasData(initialUser);
|
||||||
|
await userManager.AddToRoleAsync(initialUser, roles[0]);
|
||||||
|
|
||||||
//modelBuilder.Entity<User>().HasData(
|
//modelBuilder.Entity<User>().HasData(
|
||||||
// new User
|
// new User
|
||||||
|
|||||||
@@ -10,8 +10,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
|||||||
namespace Data.Migrations
|
namespace Data.Migrations
|
||||||
{
|
{
|
||||||
[DbContext(typeof(RentACarDbContext))]
|
[DbContext(typeof(RentACarDbContext))]
|
||||||
[Migration("20220401132719_initial")]
|
[Migration("20220401141638_InitialMigration")]
|
||||||
partial class initial
|
partial class InitialMigration
|
||||||
{
|
{
|
||||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||||
{
|
{
|
||||||
@@ -99,9 +99,6 @@ namespace Data.Migrations
|
|||||||
b.Property<string>("FirstName")
|
b.Property<string>("FirstName")
|
||||||
.HasColumnType("nvarchar(max)");
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
b.Property<bool>("IsAdmin")
|
|
||||||
.HasColumnType("bit");
|
|
||||||
|
|
||||||
b.Property<string>("LastName")
|
b.Property<string>("LastName")
|
||||||
.HasColumnType("nvarchar(max)");
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
@@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
|
|||||||
|
|
||||||
namespace Data.Migrations
|
namespace Data.Migrations
|
||||||
{
|
{
|
||||||
public partial class initial : Migration
|
public partial class InitialMigration : Migration
|
||||||
{
|
{
|
||||||
protected override void Up(MigrationBuilder migrationBuilder)
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
{
|
{
|
||||||
@@ -72,7 +72,6 @@ namespace Data.Migrations
|
|||||||
FirstName = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
FirstName = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||||
LastName = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
LastName = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||||
PersonalNumber = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
PersonalNumber = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||||
IsAdmin = table.Column<bool>(type: "bit", nullable: false),
|
|
||||||
UserName = table.Column<string>(type: "nvarchar(450)", nullable: true),
|
UserName = table.Column<string>(type: "nvarchar(450)", nullable: true),
|
||||||
NormalizedUserName = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
NormalizedUserName = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||||
Email = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
Email = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||||
@@ -97,9 +97,6 @@ namespace Data.Migrations
|
|||||||
b.Property<string>("FirstName")
|
b.Property<string>("FirstName")
|
||||||
.HasColumnType("nvarchar(max)");
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
b.Property<bool>("IsAdmin")
|
|
||||||
.HasColumnType("bit");
|
|
||||||
|
|
||||||
b.Property<string>("LastName")
|
b.Property<string>("LastName")
|
||||||
.HasColumnType("nvarchar(max)");
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ namespace WebApp.Areas.Identity.Pages.Account
|
|||||||
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
|
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
|
||||||
if (ModelState.IsValid)
|
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);
|
var result = await _userManager.CreateAsync(user, Input.Password);
|
||||||
if (result.Succeeded)
|
if (result.Succeeded)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -68,7 +68,6 @@ namespace API
|
|||||||
|
|
||||||
app.UseAuthentication();
|
app.UseAuthentication();
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
CreateUserRoles(serviceProvider).Wait();
|
|
||||||
app.UseEndpoints(endpoints =>
|
app.UseEndpoints(endpoints =>
|
||||||
{
|
{
|
||||||
endpoints.MapControllerRoute(
|
endpoints.MapControllerRoute(
|
||||||
@@ -77,43 +76,5 @@ namespace API
|
|||||||
endpoints.MapRazorPages();
|
endpoints.MapRazorPages();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private async System.Threading.Tasks.Task CreateUserRoles(IServiceProvider serviceProvider)
|
|
||||||
{
|
|
||||||
var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
|
|
||||||
var UserManager = serviceProvider.GetRequiredService<UserManager<User>>();
|
|
||||||
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");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=RentACar;Integrated Security=true;"
|
"DefaultConnection": "Server=.;Database=RentACar;Integrated Security=true;"
|
||||||
},
|
},
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=RentACar;Integrated Security=true;"
|
"DefaultConnection": "Server=.;Database=RentACar;Integrated Security=true;"
|
||||||
},
|
},
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
|
|||||||
Reference in New Issue
Block a user