public async Task WontAddUserToRoleTwice(string roleName) { testUser.AddToRole(roleName); await userStore.AddToRoleAsync(testUser, roleName, default(CancellationToken)); Assert.Equal(1, testUser.Roles.Count(r => r == roleName)); }
public async Task <IActionResult> Register(RegisterModel newCustomer) { if (ModelState.IsValid) { var customer = new IdentityUser { PhoneNumber = newCustomer.Phone, Email = newCustomer.Email, Name = newCustomer.Email }; var user = await userManager.FindByEmailAsync(newCustomer.Email); if (user == null) { bool phoneExists = await usersService.IsPhoneNumberExists(newCustomer.Phone); if (!phoneExists) { customer.AddToRole("Customer"); var result = await userManager.CreateAsync(customer, newCustomer.Password); if (result.Succeeded) { var code = await userManager.GenerateEmailConfirmationTokenAsync(customer); var callbackUrl = MakeAction(nameof(ConfirmEmail), "Profile", new { userId = customer.Id, code = code }); string emailBody = await viewRender.RenderToStringAsync("Templates/ActivationLetterBody", callbackUrl); sendEmail.Send(new EmailLetter { To = customer.Email, Subject = "Активация регистрации", Body = emailBody }); return(RedirectToAction(nameof(RegisterOK))); } else { AddErrors(result.Errors); } } else { ModelState.AddModelError("", "Пользователь с таким телефоном уже зарегистрирован."); } } else { ModelState.AddModelError("", "Пользователь с таким e-mail уже зарегистрирован."); } } return(View()); }
public UserStoreTests(MongoHelper mongoHelper) { testUser = new IdentityUser { Id = Guid.NewGuid().ToString(), Name = "name", NormalizedName = "normalizedName", PasswordHash = "hashypash", Email = "*****@*****.**", NormalizedEmail = "*****@*****.**", EmailConfirmed = true, SecurityStamp = "stampy", LockoutEndDate = new DateTimeOffset(DateTime.Now), PhoneNumber = "222222222222" }; testUser.AddLogin(new PersistedUserLoginInfo("gwar", "123")); testUser.AddClaim(new PersistedClaim("test", "data")); testUser.SetToken(new AuthToken("goog.goo", "garbage", "data")); testUser.AddToRole("blarg"); db = mongoHelper.Database; usersCollection = mongoHelper.Users; userStore = new MongoUserStore <IdentityUser>(usersCollection); }