public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { var user = new User { UserName = model.Username, Email = model.Email, Name = model.Name, Gender = model.Gender, Birthdate = model.Birthdate, Location = model.Location }; using (var memoryStream = new MemoryStream()) { if (model.ProfilePhoto == null) { var imgToArr = ImageConvertions.ImagePathToArray(environment.WebRootPath + "/images/default-profile.png"); user.ProfilePhoto = imgToArr; } else { await model.ProfilePhoto.CopyToAsync(memoryStream); user.ProfilePhoto = memoryStream.ToArray(); } } var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); //var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme); //await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl); await _signInManager.SignInAsync(user, isPersistent : false); _logger.LogInformation("User created a new account with password."); return(RedirectToLocal(returnUrl)); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public static IApplicationBuilder UseDatabaseMigration(this IApplicationBuilder app) { using (var serviceScope = app.ApplicationServices.GetRequiredService <IServiceScopeFactory>().CreateScope()) { serviceScope.ServiceProvider.GetService <MeowDbContext>().Database.Migrate(); var userManager = serviceScope.ServiceProvider.GetService <UserManager <User> >(); var roleManager = serviceScope.ServiceProvider.GetService <RoleManager <IdentityRole> >(); Task .Run(async() => { var adminName = WebConstants.AdministratorRole; var volunteerName = WebConstants.VolunteerRole; var defaultBirthdate = "08/08/1990"; var defaultProfilePhoto = WebConstants.DefaultProfilePhotoUrl; // admin, volunteer, normal user var roles = new[] { adminName, volunteerName }; foreach (var role in roles) { var roleExists = await roleManager.RoleExistsAsync(role); if (!roleExists) { await roleManager.CreateAsync(new IdentityRole { Name = role }); } } var adminEmail = "*****@*****.**"; var adminUser = await userManager.FindByEmailAsync(adminEmail); if (adminUser == null) { adminUser = new User { Email = adminEmail, UserName = adminName, Name = adminName, Gender = Gender.Female, Birthdate = DateTime.ParseExact(defaultBirthdate, "MM/dd/yyyy", CultureInfo.CreateSpecificCulture("en-US")), Location = "Sofia", ProfilePhoto = ImageConvertions.ImageUrlToArray(defaultProfilePhoto) }; var result = await userManager.CreateAsync(adminUser, "admin11"); await userManager.AddToRoleAsync(adminUser, adminName); await userManager.AddToRoleAsync(adminUser, volunteerName); } var volunteerEmail = "*****@*****.**"; var volunteerUser = await userManager.FindByEmailAsync(volunteerEmail); if (volunteerUser == null) { volunteerUser = new User { Email = volunteerEmail, UserName = volunteerName, Name = volunteerName, Gender = Gender.Male, Birthdate = DateTime.ParseExact(defaultBirthdate, "MM/dd/yyyy", CultureInfo.CreateSpecificCulture("en-US")), Location = "Sofia", ProfilePhoto = ImageConvertions.ImageUrlToArray(defaultProfilePhoto) }; var result = await userManager.CreateAsync(volunteerUser, "icatrescue1"); await userManager.AddToRoleAsync(volunteerUser, volunteerName); } // how narcissistic var mirelkaEmail = "*****@*****.**"; var mirelkaUser = await userManager.FindByEmailAsync(mirelkaEmail); var mirelkaBirthdate = "07/25/1995"; var mirelkaDefaultProfilePhoto = WebConstants.DefaultMirelkaProfilePhotoUrl; if (mirelkaUser == null) { mirelkaUser = new User { Email = mirelkaEmail, UserName = "******", Name = "Mirelka", Gender = Gender.Female, Birthdate = DateTime.ParseExact(mirelkaBirthdate, "MM/dd/yyyy", CultureInfo.CreateSpecificCulture("en-US")), Location = "Sofia", ProfilePhoto = ImageConvertions.ImageUrlToArray(mirelkaDefaultProfilePhoto) }; var result = await userManager.CreateAsync(mirelkaUser, "mirelka1"); } }) .Wait(); } return(app); }