Crash on register fixed
Added ID generation in the RegisterModel
This commit is contained in:
@@ -28,35 +28,33 @@ namespace Data
|
||||
{
|
||||
|
||||
base.OnModelCreating(modelBuilder);
|
||||
//string[] roles = { "Admin", "Employee" };
|
||||
|
||||
this.Database.EnsureCreated();
|
||||
string[] roles = { "Admin", "Employee" };
|
||||
//foreach (string role in roles)
|
||||
//{
|
||||
// IdentityRole roleToCheck = await this.Roles.FirstOrDefaultAsync(roleToCheck => roleToCheck.Name == role);
|
||||
// if (roleToCheck == null)
|
||||
// {
|
||||
// //this.Roles.Add(new IdentityRole(role));
|
||||
// modelBuilder.Entity<IdentityRole>().HasData(new IdentityRole(role));
|
||||
// }
|
||||
//}
|
||||
|
||||
foreach (string role in roles)
|
||||
{
|
||||
IdentityRole roleToCheck = await this.Roles.FirstOrDefaultAsync(roleToCheck => roleToCheck.Name == role);
|
||||
if (roleToCheck == null)
|
||||
{
|
||||
//this.Roles.Add(new IdentityRole(role));
|
||||
modelBuilder.Entity<IdentityRole>().HasData(new IdentityRole(role));
|
||||
}
|
||||
}
|
||||
//PasswordHasher<User> passwordHasher = new PasswordHasher<User>();
|
||||
|
||||
PasswordHasher<User> passwordHasher = new PasswordHasher<User>();
|
||||
|
||||
User initialUser = new User();
|
||||
initialUser.Id = Guid.NewGuid().ToString();
|
||||
initialUser.UserName = "admin";
|
||||
initialUser.PasswordHash = passwordHasher.HashPassword(initialUser, "admin");
|
||||
//User initialUser = new User();
|
||||
//initialUser.Id = Guid.NewGuid().ToString();
|
||||
//initialUser.UserName = "admin";
|
||||
//initialUser.PasswordHash = passwordHasher.HashPassword(initialUser, "admin");
|
||||
|
||||
|
||||
if (this.Users.FirstOrDefaultAsync() != null)
|
||||
{
|
||||
//if (this.Users.FirstOrDefaultAsync() != null)
|
||||
//{
|
||||
|
||||
modelBuilder.Entity<User>().HasData(initialUser);
|
||||
IdentityRole<string> adminRole = await this.Roles.FirstOrDefaultAsync(role => role.Name == "Admin");
|
||||
modelBuilder.Entity<IdentityUserRole<string>>().HasData(new IdentityUserRole<string> {RoleId = adminRole.Id, UserId = initialUser.Id});
|
||||
}
|
||||
// modelBuilder.Entity<User>().HasData(initialUser);
|
||||
// IdentityRole<string> adminRole = await this.Roles.FirstOrDefaultAsync(role => role.Name == "Admin");
|
||||
// modelBuilder.Entity<IdentityUserRole<string>>().HasData(new IdentityUserRole<string> {RoleId = adminRole.Id, UserId = initialUser.Id});
|
||||
//}
|
||||
|
||||
modelBuilder.Entity<Rents>().HasOne(rents => rents.User);
|
||||
modelBuilder.Entity<Rents>().HasOne(rents => rents.Car);
|
||||
@@ -67,7 +65,6 @@ namespace Data
|
||||
Brand = "Trabant"
|
||||
}) ;
|
||||
|
||||
this.SaveChanges();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,6 +17,11 @@
|
||||
<input asp-for="Input.Email" class="form-control" />
|
||||
<span asp-validation-for="Input.Email" class="text-danger"></span>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="Input">Username</label>
|
||||
<input asp-for="Input" class="form-control" />
|
||||
<span asp-validation-for="Input" class="text-danger"></span>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="Input.Password"></label>
|
||||
<input asp-for="Input.Password" class="form-control" />
|
||||
|
||||
@@ -75,7 +75,7 @@ namespace WebApp.Areas.Identity.Pages.Account
|
||||
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
var user = new User { UserName = Input.Email, Email = Input.Email };
|
||||
var user = new User { Id = Guid.NewGuid().ToString(), UserName = Input.Email, Email = Input.Email };
|
||||
var result = await _userManager.CreateAsync(user, Input.Password);
|
||||
if (result.Succeeded)
|
||||
{
|
||||
|
||||
@@ -43,6 +43,7 @@ namespace API
|
||||
})
|
||||
.AddRoles<IdentityRole>()
|
||||
.AddDefaultUI()
|
||||
.AddDefaultTokenProviders()
|
||||
.AddEntityFrameworkStores<RentACarDbContext>();
|
||||
|
||||
services.AddControllersWithViews();
|
||||
|
||||
Reference in New Issue
Block a user