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