Example #1
0
 public async Task<OperationDetails> Create(UserDTO userDto)
 {
     ApplicationUser user = await _uow.UserManager.FindByEmailAsync(userDto.Email);
     if (user == null)
     {
         user = new ApplicationUser { Email = userDto.Email, UserName = userDto.Email };
         await _uow.UserManager.CreateAsync(user, userDto.Password);
         // добавляем роль
         await _uow.UserManager.AddToRoleAsync(user.Id, userDto.Role);
         // создаем профиль клиента
         ClientProfile clientProfile = new ClientProfile
         {
             Id = user.Id,
             LastName = userDto.LastName,
             FirstName = userDto.FirstName
         };
         _uow.ClientManager.Create(clientProfile);
         await _uow.SaveAsync();
         return new OperationDetails(true, "Registration was successful", "");
     }
     else
     {
         return new OperationDetails(false, "User with such email already exists", "Email");
     }
 }
Example #2
0
 public async Task<ClaimsIdentity> Authenticate(UserDTO userDto)
 {
     ClaimsIdentity claim = null;
     // находим пользователя
     ApplicationUser user = await _uow.UserManager.FindAsync(userDto.Email, userDto.Password);
     // авторизуем его и возвращаем объект ClaimsIdentity
     if (user != null)
         claim = await _uow.UserManager.CreateIdentityAsync(user,
                                     DefaultAuthenticationTypes.ApplicationCookie);
     return claim;
 }
Example #3
0
        public async Task<ActionResult> Login(ViewModels.Account.LoginViewModel model)
        {
            //await SetInitialDataAsync();
            if (ModelState.IsValid)
            {
                UserDTO userDto = new UserDTO { Email = model.Email, Password = model.Password };
                ClaimsIdentity claim = await UserService.Authenticate(userDto);
                if (claim == null)
                {
                    ModelState.AddModelError("", "Неверный логин или пароль.");
                }
                else
                {
                    AuthenticationManager.SignOut();
                    AuthenticationManager.SignIn(new AuthenticationProperties
                    {
                        IsPersistent = true
                    }, claim);
                    return RedirectToAction("Index", "Home");
                }
            }

            return View(model);
        }
Example #4
0
 public async Task<ActionResult> Register(ViewModels.Account.RegisterViewModel model)
 {
     // await SetInitialDataAsync();
     if (ModelState.IsValid)
     {
         UserDTO userDto = new UserDTO
         {
             Email = model.Email,
             Password = model.Password,
             PhoneNumber = model.PhoneNumber,
             FirstName = model.FirstName,
             LastName = model.LastName,
             Role = "User"
         };
         OperationDetails operationDetails = await UserService.Create(userDto);
         if (operationDetails.Success)
             return RedirectToAction("Index", "Home");
         else
             ModelState.AddModelError(operationDetails.Property, operationDetails.Message);
     }
     return View(model);
 }