Exemplo n.º 1
0
 public async Task<ClaimsIdentity> Authenticate(UserDTO userDto)
 {
     ClaimsIdentity claim = null;
     // находим пользователя
     var user = await Database.UserManager.FindAsync(userDto.Email, userDto.Password);
     // авторизуем его и возвращаем объект ClaimsIdentity
     if (user != null)
         claim = await Database.UserManager.CreateIdentityAsync(user,
                                     DefaultAuthenticationTypes.ApplicationCookie);
     return claim;
 }
Exemplo n.º 2
0
 // начальная инициализация бд
 public async Task SetInitialData(UserDTO adminDto, List<string> roles)
 {
     foreach (var roleName in roles)
     {
         var role = await Database.RoleManager.FindByNameAsync(roleName);
         if (role == null)
         {
             role = new ApplicationRole { Name = roleName };
             await Database.RoleManager.CreateAsync(role);
         }
     }
     await Create(adminDto);
 }
Exemplo n.º 3
0
        public async Task<OperationDetails> Create(UserDTO userDto)
        {
            var user = await Database.UserManager.FindByEmailAsync(userDto.Email);

            if (user != null)
                return new OperationDetails(false, "Пользователь с таким логином уже существует", "Email");

            user = new ApplicationUser { Email = userDto.Email, UserName = userDto.Email };
            await Database.UserManager.CreateAsync(user, userDto.Password);
            // добавляем роль
            await Database.UserManager.AddToRoleAsync(user.Id, userDto.Role);
            // создаем профиль клиента
            var clientProfile = new ClientProfile { Id = user.Id, Address = userDto.Address, Name = userDto.Name };
            Database.ClientManager.Create(clientProfile);
            await Database.SaveAsync();
            return new OperationDetails(true, "Регистрация успешно пройдена", "");
        }
Exemplo n.º 4
0
 public async Task<ActionResult> Login(LoginModel model)
 {
     await SetInitialDataAsync();
     if (ModelState.IsValid)
     {
         var userDto = new UserDTO { Email = model.Email, Password = model.Password };
         var 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);
 }
Exemplo n.º 5
0
        public async Task<ActionResult> Register(RegisterModel model)
        {
            await SetInitialDataAsync();
            if (ModelState.IsValid)
            {
                var userDto = new UserDTO
                {
                    Email = model.Email,
                    Password = model.Password,
                    Address = model.Address,
                    Name = model.Name,
                    Role = "user"
                };
                var operationDetails = await UserService.Create(userDto);

                if (operationDetails.Succedeed)
                    return View("SuccessRegister");
                
                ModelState.AddModelError(operationDetails.Property, operationDetails.Message);
            }
            return View(model);
        }