public async Task <IActionResult> Register(Users model)
        {
            if (ModelState.IsValid)
            {
                Users user = await _context.Users.FirstOrDefaultAsync(u => u.Email == model.Email);

                if (user == null)
                {
                    user = model;
                    Roles userRole = await _context.Roles.FirstOrDefaultAsync(r => r.Name == "Citizen");

                    if (userRole != null)
                    {
                        user.Role = userRole;
                    }

                    _context.Users.Add(user);
                    await _context.SaveChangesAsync();

                    await Authenticate(user);

                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    ModelState.AddModelError("", "Некоректні логін і(або) пароль");
                }
            }
            return(View(model));
        }
Exemple #2
0
        public async Task <IActionResult> Create(Protocols protocol)
        {
            if (ModelState.IsValid)
            {
                var user_email = User.Identity.Name;
                var userId     = _context.Users.FirstOrDefault(x => x.Email == user_email).Id;

                protocol.User1Id = userId;

                _context.Add(protocol);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(CreatedProtocols)));
            }
            return(View(protocol));
        }