public async Task <IActionResult> Register(RegisterModel model) { if (ModelState.IsValid) { SprUser user = await db.SprUser.FirstOrDefaultAsync(u => u.Email == model.Email); if (user == null) { // добавляем пользователя в бд db.SprUser.Add(new SprUser { Email = model.Email, Password = model.Password }); await db.SaveChangesAsync(); await Authenticate(user); // аутентификация return(RedirectToAction("Index", "Home")); } else { ModelState.AddModelError("", "Некорректные логин и(или) пароль"); } } return(View(model)); }
public void IndexViewResultNotNull() { // TODO: реализовать в mock классе IQuerable для LINQ запросов // Arrange var mock = new Mock <StationeryContext>(); SprUser user = new SprUser(); user.LastName = "Sidorov"; user.FirstName = "Ivan"; user.Id = 100; mock.Setup(db => db.SprUser.Add(user)); SprProduct prod = new SprProduct(); prod.ProductName = "karandash"; prod.Id = 101; mock.Setup(db => db.SprProduct.Add(prod)); HomeController controller = new HomeController(mock.Object); // Act ViewResult result = controller.Index() as ViewResult; // Assert Assert.NotNull(result); }
private async Task Authenticate(SprUser user) { // создаем один claim var claims = new List <Claim> { new Claim(ClaimsIdentity.DefaultNameClaimType, user.Email), new Claim(ClaimsIdentity.DefaultRoleClaimType, user.Role?.Name) }; // создаем объект ClaimsIdentity ClaimsIdentity id = new ClaimsIdentity(claims, "ApplicationCookie", ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType); // установка аутентификационных куки await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(id)); }
public async Task <IActionResult> Login(LoginModel model) { if (ModelState.IsValid) { SprUser user = await db.SprUser.Include(u => u.Role).FirstOrDefaultAsync(u => u.Email == model.Email && u.Password == model.Password); if (user != null) { await Authenticate(user); // аутентификация return(RedirectToAction("Index", "Home")); } ModelState.AddModelError("", "Некорректные логин и(или) пароль"); } return(View(model)); }