public async Task<ActionResult> Register(string returnUrl) { string email = null; if (!string.IsNullOrEmpty(returnUrl)) { var uri = returnUrl.Replace("%40", "@"); var emailRegex = new Regex("userEmail=(?<email>.*@.*)&permission"); email = emailRegex.Match(uri).Groups["email"].Value; } var model = new RegisterWithPasswordViewModel { Currencies = await this.GetCurrencies(), ReturnUrl = returnUrl, Email = email, IsExternal = !string.IsNullOrEmpty(email) }; return this.View(model); }
public async Task<ActionResult> Register(RegisterWithPasswordViewModel model, string returnUrl) { this.IsCaptchaValid(SharedResource.CaptchaValidationFailed); if (!ModelState.IsValid) { this.AddError(SharedResource.ModelStateIsNotValid); model.Currencies = await this.GetCurrencies(); return this.View(model); } var user = await this.CreateUser(model); var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInManager.SignInAsync(user, false, false); var userRole = await RoleManager.FindByNameAsync(UserIdentity.UserRole); await UserManager.AddToRoleAsync(user.Id, userRole.Name); if (string.IsNullOrWhiteSpace(returnUrl)) return this.RedirectToAction(SharedConstant.Index, SharedConstant.DashBoard); return this.RedirectToLocal(returnUrl); } this.AddErrors(result); model.Currencies = await this.GetCurrencies(); // If we got this far, something failed, redisplay form return this.View(model); }
public async Task<ActionResult> Create(RegisterWithPasswordViewModel userWithPasswordViewModel) { if (!ModelState.IsValid) { this.AddError(SharedResource.ModelStateIsNotValid); userWithPasswordViewModel.RolesList = await this.GetAllRolesAsync(); userWithPasswordViewModel.Currencies = await this.GetCurrencies(); return this.View(userWithPasswordViewModel); } if (userWithPasswordViewModel.SelectedRoles == null) { ModelState.AddModelError("", UsersAdminResource.OneRoleMustBeSelected); userWithPasswordViewModel.RolesList = await this.GetAllRolesAsync(); userWithPasswordViewModel.Currencies = await this.GetCurrencies(); return this.View(userWithPasswordViewModel); } var user = await this.CreateUser(userWithPasswordViewModel); var adminresult = await UserManager.CreateAsync(user, userWithPasswordViewModel.Password); //Add User to the selected SelectedRoles if (adminresult.Succeeded) { if (userWithPasswordViewModel.SelectedRoles == null) return this.RedirectToAction(SharedConstant.Index); var result = await UserManager.AddToRolesAsync(user.Id, userWithPasswordViewModel.SelectedRoles.ToArray()); if (!result.Succeeded) { result.Errors.ForEach(error => ModelState.AddModelError("", error)); userWithPasswordViewModel.RolesList = await this.GetAllRolesAsync(); return this.View(userWithPasswordViewModel); } } else { adminresult.Errors.ForEach(e => ModelState.AddModelError("", e)); userWithPasswordViewModel.RolesList = await this.GetAllRolesAsync(); userWithPasswordViewModel.Currencies = await this.GetCurrencies(); return this.View(userWithPasswordViewModel); } this.AddSuccess(string.Format(UsersAdminResource.UserCreated, userWithPasswordViewModel.Email)); return this.RedirectToAction(SharedConstant.Index); }