public async Task<ActionResult> Create(RegisterViewModel userViewModel, params string[] selectedRoles) { if (ModelState.IsValid) { var user = new User { UserName = userViewModel.Email, Email = userViewModel.Email, CreationDate = DateTime.Now, PersonalWallet = new Wallet { Name = "Default Wallet", Currency = new Currency { Name = "Česká koruna", Symbol = "Kč" } } }; var adminresult = await UserManager.CreateAsync(user, userViewModel.Password); //Add User to the selected Roles if (adminresult.Succeeded) { if (selectedRoles != null) { var result = await UserManager.AddToRolesAsync(user.Id, selectedRoles); if (!result.Succeeded) { ModelState.AddModelError("", result.Errors.First()); ViewBag.RoleId = new SelectList(await RoleManager.Roles.ToListAsync(), "Name", "Name"); return this.View(); } } } else { ModelState.AddModelError("", adminresult.Errors.First()); ViewBag.RoleId = new SelectList(RoleManager.Roles, "Name", "Name"); return this.View(); } return this.RedirectToAction("Index"); } ViewBag.RoleId = new SelectList(RoleManager.Roles, "Name", "Name"); return this.View(); }
public async Task<ActionResult> ExternalLoginConfirmation(RegisterViewModel model, string returnUrl) { if (User.Identity.IsAuthenticated) { return this.RedirectToAction(SharedConstant.Index, LoginConstant.Manage); } if (ModelState.IsValid) { // Get the information about the UserProfile from the external login provider var info = await AuthenticationManager.GetExternalLoginInfoAsync(); if (info == null) { return this.View(LoginConstant.ExternalLoginFailure); } var user = await this.CreateUser(model); var result = await UserManager.CreateAsync(user); if (result.Succeeded) { var userRole = await RoleManager.FindByNameAsync(UserIdentity.UserRole); await UserManager.AddToRoleAsync(user.Id, userRole.Name); result = await UserManager.AddLoginAsync(user.Id, info.Login); if (result.Succeeded) { await SignInManager.SignInAsync(user, false, false); return this.RedirectToLocal(returnUrl); } } this.AddErrors(result); } model.Currencies = await this.GetCurrencies(); ViewBag.ReturnUrl = returnUrl; return this.View(model); }
protected async Task<UserIdentity> CreateUser(RegisterViewModel model) { var currency = await UserContext.Currencies.FirstOrDefaultAsync(userCurrency => userCurrency.Guid == model.CurrencyId); var user = new UserIdentity { UserName = model.Email, Email = model.Email, CreationDate = DateTime.Now, Profile = new UserProfile { FirstName = model.FirstName, LastName = model.LastName } }; user.Profile.WalletAccessRights = new List<WalletAccessRight> { new WalletAccessRight { Permission = PermissionEnum.Owner, UserProfile = user.Profile, Wallet = new Wallet { Name = UserConstant.FormatWalletName( model.FirstName, model.LastName, SharedResource.DefaultWallet), Currency = currency } } }; return user; }