Esempio n. 1
0
 public async Task<IActionResult> Register(RegisterViewModel model)
 {
     if (!AppSettings.AllowUserRegistration)
     {
         return HttpNotFound();
     }
     if (ModelState.IsValid)
     {
         var user = new ApplicationUser {UserName = model.Email, Email = model.Email};
         var result = await CreateNewUser(user, model);
         if (result.Succeeded)
         {
             // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713
             // Send an email with this link
             //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
             //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
             //await _emailSender.SendEmailAsync(model.Email, "Confirm your account",
             //    "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");
             await _signInManager.SignInAsync(user, false);
             _logger.LogInformation(3, "User created a new account with password.");
             return RedirectToAction(nameof(HomeController.Index), "Home");
         }
         AddErrors(result);
     }
     // If we got this far, something failed, redisplay form
     return View(model);
 }
Esempio n. 2
0
 /// <summary>
 /// This method takes care that the first user to be created in the database is granted Admin rights.
 /// </summary>
 /// <param name="user"></param>
 /// <param name="model"></param>
 /// <returns></returns>
 public async Task<IdentityResult> CreateNewUser(ApplicationUser user, RegisterViewModel model)
 {
     var result = await _userManager.CreateAsync(user, model.Password);
     if (result.Succeeded)
     {
         // Add user to admin role if it's the first registered user
         if (Context.Users.Count() == 1)
         {
             await _userManager.AddToRoleAsync(user, "Admin");
         }
     }
     return result;
 }