public async Task <IdentityResult> Register(RegisterInputModel inputModel) { var user = new F1CafeUser { UserName = inputModel.UserName, FirstName = inputModel.FirstName, LastName = inputModel.LastName, Email = inputModel.Email }; var registerUserResult = await this.userManager.CreateAsync(user, inputModel.Password); if (registerUserResult.Succeeded) { this.logger.LogInformation("User created a new account with password."); await AssingRoleToUser(user); await this.signInManager.SignInAsync(user, isPersistent : false); return(IdentityResult.Success); } return(registerUserResult); }
private async Task AssingRoleToUser(F1CafeUser user) { if (this.userManager.Users.Count() > 1) { await this.userManager.AddToRoleAsync(user, GlobalConstants.UserRoleName); } else { await this.userManager.AddToRoleAsync(user, GlobalConstants.AdministratorRoleName); } }
public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); // Get the information about the user from the external login provider var info = await this.signInManager.GetExternalLoginInfoAsync(); if (info == null) { this.ErrorMessage = "Error loading external login information during confirmation."; return(this.RedirectToPage("./Login", new { ReturnUrl = returnUrl })); } if (this.ModelState.IsValid) { var user = new F1CafeUser { UserName = Input.Email, Email = Input.Email }; var result = await userManager.CreateAsync(user); if (result.Succeeded) { result = await userManager.AddLoginAsync(user, info); if (result.Succeeded) { await this.signInManager.SignInAsync(user, isPersistent : false); this.logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider); return(this.LocalRedirect(returnUrl)); } } foreach (var error in result.Errors) { this.ModelState.AddModelError(string.Empty, error.Description); } } this.LoginProvider = info.LoginProvider; this.ReturnUrl = returnUrl; return(this.Page()); }