public ActionResult Register(AccountRegisterInputModel input, string returnUrl) { if (ModelState.IsValid) { var result = this.accountAdapter.RegisterUser(new User() { Username = input.UserName, Email = input.Email, FirstName = input.FirstName, LastName = input.LastName }, input.Password); if (result.StatusCode == 200) { FormsAuthentication.SetAuthCookie(result.Result.Username, false); // redirect the user if (String.IsNullOrEmpty(returnUrl)) { return(Redirect("/")); } else { return(Redirect(returnUrl)); } } var error = result.Errors.First(); ModelState.AddModelError(error.MemberNames.First(), error.ErrorMessage); } return(View(new AccountRegisterModel() { Input = input })); }
public async Task <IActionResult> Register(AccountRegisterInputModel model) { if (!this.ModelState.IsValid) { return(this.View(model)); } SignInResult result; try { result = await this.accountService.Register(model); } catch (Exception e) { return(this.Json(e.Message)); } if (!result.Succeeded) { return(this.View(model)); } return(this.Redirect("/")); }
public ActionResult Register(AccountRegisterInputModel input, string returnUrl) { if (ModelState.IsValid) { var result = this.accountAdapter.RegisterUser(new User() { Username = input.UserName, Email = input.Email, FirstName = input.FirstName, LastName = input.LastName }, input.Password); if (result.StatusCode == 200) { CustomAuthentication.SetAuthCookie(result.Result.Username, result.Result.UserId, false); // redirect the user if (String.IsNullOrEmpty(returnUrl)) { return(Redirect("/")); } else { return(Redirect(returnUrl)); } } HandleErrors(result); } return(View(new AccountRegisterModel() { Input = input })); }
public async Task <SignInResult> Register(AccountRegisterInputModel model) { var user = new PandaUser { UserName = model.Username, Email = model.Email }; var registerResult = await this.userManager.CreateAsync(user, model.Password); if (!registerResult.Succeeded) { throw new Exception(string.Join(Environment.NewLine, registerResult.Errors)); } var adminRoleExists = await this.roleManager.RoleExistsAsync("Admin"); if (!adminRoleExists) { await this.roleManager.CreateAsync(new PandaUserRole { Name = "Admin" }); } var userRoleExists = await this.roleManager.RoleExistsAsync("User"); if (!userRoleExists) { await this.roleManager.CreateAsync(new PandaUserRole { Name = "User" }); } var usersCount = this.userManager.Users.Count(); if (usersCount == 1) { await this.userManager.AddToRoleAsync(user, "Admin"); } else { await this.userManager.AddToRoleAsync(user, "User"); } var loginModel = new AccountLoginInputModel { Username = model.Username, Password = model.Password, RememberMe = false }; var result = await this.Login(loginModel); return(result); }