コード例 #1
0
        public async Task ChangePasswordAsync(UserForIdentity userDto, string NewPassword)
        {
            ApplicationUser user = await Database.UserManager.FindByEmailAsync(userDto.Email);

            ClientProfile clientProfile = new ClientProfile {
                Id = user.Id, Name = userDto.Name
            };

            Database.ClientManager.Delete(clientProfile);
            var result = await Database.UserManager.DeleteAsync(user);

            if (result.Errors.Count() > 0)
            {
                new OperationDetails(false, result.Errors.FirstOrDefault(), "");
            }
            await Database.SaveAsync();

            user = new ApplicationUser {
                Email = userDto.Email, UserName = userDto.Email
            };
            result = await Database.UserManager.CreateAsync(user, NewPassword);

            await Database.UserManager.AddToRoleAsync(user.Id, "user");

            clientProfile = new ClientProfile {
                Id = user.Id, Name = userDto.Name
            };
            Database.ClientManager.Create(clientProfile);
            await Database.SaveAsync();
        }
コード例 #2
0
        public async Task <OperationDetails> Create(UserForIdentity 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, Name = userDto.Name
                };
                Database.ClientManager.Create(clientProfile);
                await Database.SaveAsync();

                return(new OperationDetails(true, "Регистрация успешно пройдена", ""));
            }
            else
            {
                return(new OperationDetails(false, "Пользователь с таким логином уже существует", "Email"));
            }
        }
コード例 #3
0
ファイル: AccountController.cs プロジェクト: Hoteng1/ImagePin
        public async Task <ActionResult> Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                UserForIdentity userForIdentity = new UserForIdentity
                {
                    Email    = model.Email,
                    Password = model.Password,
                    Name     = model.Login,
                    Role     = "user"
                };
                OperationDetails operationDetails = await UserService.Create(userForIdentity);

                if (operationDetails.Succedeed)
                {
                    service.CreateUser(new BLL.EntitesDTO.UserDTO {
                        Email = model.Login, Login = model.Email
                    });
                    ClaimsIdentity claim = await UserService.Authenticate(userForIdentity);

                    AuthenticationManager.SignOut();
                    AuthenticationManager.SignIn(new AuthenticationProperties
                    {
                        IsPersistent = true
                    }, claim);
                    return(View("SuccessRegister"));
                }
                else
                {
                    ModelState.AddModelError(operationDetails.Property, operationDetails.Message);
                }
            }
            return(View(model));
        }
コード例 #4
0
ファイル: AccountController.cs プロジェクト: Hoteng1/ImagePin
        public async Task <ActionResult> Login(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                UserForIdentity userForIdentity = new UserForIdentity {
                    Email = model.Email, Password = model.Password
                };
                ClaimsIdentity claim = await UserService.Authenticate(userForIdentity);

                if (claim == null)
                {
                    ModelState.AddModelError("", "Неверный логин или пароль.");
                }
                else
                {
                    AuthenticationManager.SignOut();
                    AuthenticationManager.SignIn(new AuthenticationProperties
                    {
                        IsPersistent = true
                    }, claim);
                    return(RedirectToAction("Index", "Home"));
                }
            }
            return(View(model));
        }
コード例 #5
0
ファイル: HomeController.cs プロジェクト: Hoteng1/ImagePin
        public async Task <ActionResult> DeleteUser()
        {
            UserForIdentity userForIdentity = new UserForIdentity
            {
                Email = AuthenticationManager.User.Identity.Name,
                Name  = AuthenticationManager.User.Identity.Name,
                Role  = "user"
            };
            await UserService.DeleteUserAsync(userForIdentity);

            service.DeleteUser(AuthenticationManager.User.Identity.Name);
            AuthenticationManager.SignOut();
            return(View("Index"));
        }
コード例 #6
0
        public async Task <ClaimsIdentity> Authenticate(UserForIdentity userDto)
        {
            ClaimsIdentity claim = null;
            // находим пользователя
            ApplicationUser user = await Database.UserManager.FindAsync(userDto.Email, userDto.Password);

            // авторизуем его и возвращаем объект ClaimsIdentity
            if (user != null)
            {
                claim = await Database.UserManager.CreateIdentityAsync(user,
                                                                       DefaultAuthenticationTypes.ApplicationCookie);
            }
            return(claim);
        }
コード例 #7
0
ファイル: HomeController.cs プロジェクト: Hoteng1/ImagePin
        public async Task <ActionResult> DeleteUsers(string userName)
        {
            if (AuthenticationManager.User.IsInRole("admin") && userName != null)
            {
                UserForIdentity userForIdentity = new UserForIdentity
                {
                    Email = userName,
                    Name  = userName,
                    Role  = "user"
                };
                await UserService.DeleteUserAsync(userForIdentity);

                service.DeleteUser(userName);
            }
            return(RedirectToAction("ManagedUser"));
        }
コード例 #8
0
        // начальная инициализация бд
        public async Task SetInitialData(UserForIdentity 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);
        }
コード例 #9
0
ファイル: HomeController.cs プロジェクト: Hoteng1/ImagePin
        public async Task <ActionResult> ChangePassword(string OldPassword, string NewPassword)
        {
            UserForIdentity userForIdentity = new UserForIdentity {
                Email = AuthenticationManager.User.Identity.Name, Password = OldPassword
            };
            ClaimsIdentity claim = await UserService.Authenticate(userForIdentity);

            if (claim == null)
            {
                ModelState.AddModelError("", "Неверный логин или пароль.");
            }
            else
            {
                await UserService.ChangePasswordAsync(userForIdentity, NewPassword);
            }

            return(RedirectToAction("Profiles"));
        }
コード例 #10
0
        public async Task DeleteUserAsync(UserForIdentity userDto)
        {
            ApplicationUser user = await Database.UserManager.FindByEmailAsync(userDto.Email);

            ClientProfile clientProfile = new ClientProfile {
                Id = user.Id, Name = userDto.Name
            };

            Database.ClientManager.Delete(clientProfile);
            var result = await Database.UserManager.DeleteAsync(user);

            if (result.Errors.Count() > 0)
            {
                new OperationDetails(false, result.Errors.FirstOrDefault(), "");
            }

            await Database.SaveAsync();
        }