Exemple #1
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            await SetInitialDataAsync();

            if (ModelState.IsValid)
            {
                RegistrationModelDto userDto = new RegistrationModelDto
                {
                    Email     = model.Email,
                    Password  = model.Password,
                    FirstName = model.FirstName,
                    LastName  = model.LastName,
                    Role      = "user"
                };
                OperationDetails operationDetails = await UserService.Create(userDto);

                if (operationDetails.Succedeed)
                {
                    return(RedirectToAction("Login"));
                }
                else
                {
                    ModelState.AddModelError(operationDetails.Property, operationDetails.Message);
                }
            }
            return(View(model));
        }
Exemple #2
0
        public async Task <ActionResult> Login(LoginViewModel model)
        {
            await SetInitialDataAsync();

            if (ModelState.IsValid)
            {
                RegistrationModelDto userDto = new RegistrationModelDto {
                    Email = model.Email, Password = model.Password
                };
                //получаем объект ClaimsIdentity, который затем используется для создания аутентификационного тикета,
                //сохраняемого в куках.
                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));
        }
Exemple #3
0
        public async Task <OperationDetails> Create(RegistrationModelDto userDto)
        {
            ApplicationUser user = await Database.UserManager.FindByEmailAsync(userDto.Email);

            if (user == null)
            {
                user = new ApplicationUser {
                    Email = userDto.Email, UserName = userDto.Email
                };
                var result = await Database.UserManager.CreateAsync(user, userDto.Password);

                if (result.Errors.Count() > 0)
                {
                    return(new OperationDetails(false, result.Errors.FirstOrDefault(), ""));
                }
                await Database.UserManager.AddToRoleAsync(user.Id, userDto.Role);

                ClientProfile clientProfile = new ClientProfile {
                    Id = user.Id, FirstName = userDto.FirstName, LastName = userDto.LastName
                };
                Database.ClientProfiles.Create(clientProfile);
                await Database.SaveAsync();

                return(new OperationDetails(true, "Registration completed successfully", ""));
            }
            else
            {
                return(new OperationDetails(false, "User with such login exists", "Email"));
            }
        }
Exemple #4
0
        public async Task <ClaimsIdentity> Authenticate(RegistrationModelDto userDto)
        {
            ClaimsIdentity  claim = null;
            ApplicationUser user  = await Database.UserManager.FindAsync(userDto.Email, userDto.Password);

            if (user != null)
            {
                claim = await Database.UserManager.CreateIdentityAsync(user,
                                                                       DefaultAuthenticationTypes.ApplicationCookie);
            }
            return(claim);
        }
Exemple #5
0
        public async Task SetInitialData(RegistrationModelDto adminDto, List <string> roles)
        {
            foreach (string 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);
        }