public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { ClientProfileDto clientDto = new ClientProfileDto { Email = model.Email, Password = model.Password, Name = model.UserName, UserName = model.UserName, }; OperationDetails operationDetails = await clientSevice.CreateUser(clientDto); if (operationDetails.Succedeed) { ClaimsIdentity claim = await clientSevice.Authenticate(clientDto); AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = true }, claim); return(RedirectToAction("Index", "Home")); } else { ModelState.AddModelError(operationDetails.Property, operationDetails.Message); } } return(View(model)); }
public async Task <OperationDetails> CreateUser(ClientProfileDto item) { var user = await dbIdentity.UserManager.FindByEmailAsync(item.Email); if (user == null) { user = new ApplicationUser { Email = item.Email, UserName = item.Email }; var result = await dbIdentity.UserManager.CreateAsync(user, item.Password); if (result.Errors.Count() > 0) { return(new OperationDetails(false, result.Errors.FirstOrDefault(), "")); } await dbIdentity.UserManager.AddToRoleAsync(user.Id, "user"); ClientProfile clientProfile = new ClientProfile { ClientProfileID = user.Id, Name = item.Name }; dbIdentity.ClientManager.Create(clientProfile); await dbIdentity.SaveAsync(); return(new OperationDetails(true, "Регистрация прошла успешно", "")); } else { return(new OperationDetails(false, "Польователь с таким логином уже существует", "Email")); } }
public async Task <ActionResult> Login(LoginViewModel model) { if (ModelState.IsValid) { ClientProfileDto userDto = new ClientProfileDto { Email = model.Email, Password = model.Password }; ClaimsIdentity claim = await clientSevice.Authenticate(userDto); if (claim == null) { ModelState.AddModelError("", "Неверный логин или пароль"); } else { AuthenticationManager.SignOut(); AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = true }, claim); return(RedirectToAction("Index", "Home")); } } return(View(model)); }
public async Task <ActionResult> Register(RegisterModel model) { await SetInitialDataAsync(); if (ModelState.IsValid) { var clientProfileDto = new ClientProfileDto { Email = model.Email, Password = model.Password, Address = model.Address, Name = model.Name, Role = "user" }; var operationDetails = await UserService.Create(clientProfileDto); if (operationDetails.Succedeed) { return(RedirectToAction("ElectronicSubscriptions", "Library")); } else { ModelState.AddModelError(operationDetails.Property, operationDetails.Message); } } return(View(model)); }
public void UpdateUser(ClientProfileDto clientProfileDto) { var cp = Database.ClientProfiles.Find(c => c.Id == clientProfileDto.Id).DefaultIfEmpty().First(); if (cp == null) { return; } cp.FirstName = clientProfileDto.FirstName; cp.LastName = clientProfileDto.LastName; cp.DateOfBirth = clientProfileDto.DateOfBirth; cp.PhoneNumber = clientProfileDto.PhoneNumber; cp.Gender = clientProfileDto.Gender; foreach (var postDto in clientProfileDto.UserPosts) { var post = new Post() { Text = postDto.Text, PostDate = postDto.PostDate, ApplicationUser = Database.UserManager.FindById(postDto.ApplicationUserId) }; cp.UserPosts.Add(post); } //cp.UserPosts = Mapper.Map<IEnumerable<PostDto>, IEnumerable<Post>>(clientProfileDto.UserPosts).ToList(); foreach (var frientDto in clientProfileDto.Friends) { var cpFriend = Database.ClientProfiles.Find(c => c.Id == frientDto.Id).DefaultIfEmpty().First(); cp.Friends.Add(cpFriend); } Database.ClientProfiles.Update(cp); Database.Save(); }
public async Task <ActionResult> Login(LoginModel model) { await SetInitialDataAsync(); if (ModelState.IsValid) { var clientProfileDto = new ClientProfileDto { Email = model.Email, Password = model.Password }; var claim = await UserService.Authenticate(clientProfileDto); if (claim == null) { ModelState.AddModelError("", "Wrong login or password."); } else { AuthenticationManager.SignOut(); AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = true }, claim); return(RedirectToAction("ElectronicSubscriptions", "Library")); } } return(View(model)); }
public async Task <ClaimsIdentity> Authenticate(ClientProfileDto item) { ClaimsIdentity claim = null; ApplicationUser user = await dbIdentity.UserManager.FindAsync(item.Email, item.Password); if (user != null) { claim = await dbIdentity.UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); } return(claim); }
//Converter ClientProfileDto -> AccountProfileVM public static AccountProfileVM FromAccountProfileDto(ClientProfileDto m) { return(new AccountProfileVM { Id = m.Id, Email = m.Email, FirstName = m.FirstName, LastName = m.LastName, PhoneNumber = m.PhoneNumber, Photo = m.Photo, ClientRate = m.ClientRate }); }
// Initial database initialization. public async Task SetInitialData(ClientProfileDto 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); }
public async Task <ClaimsIdentity> Authenticate(ClientProfileDto clientProfileDto) { ClaimsIdentity claim = null; // Find user. ApplicationUser user = await Database.UserManager.FindAsync(clientProfileDto.Email, clientProfileDto.Password); // Authorize it and return the object ClaimsIdentity. if (user != null) { claim = await Database.UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); CurrentUser = new ClientProfileDto { Id = user.Id, }; } return(claim); }
public async Task <Guid> UpdateClientProfile(ClientProfileDto clientProfileData) { try { var savedClientProfile = _dbContext.ClientProfiles.AsNoTracking().FirstOrDefault(p => p.Id == clientProfileData.Id); if (savedClientProfile == null) { return(Guid.Empty); } _dbContext.Entry(_mapper.Map <ClientProfile>(clientProfileData)).State = EntityState.Modified; await _dbContext.SaveChangesAsync(); return(savedClientProfile.Id); } catch (Exception e) { return(Guid.Empty); } }
public async Task <Guid> AddClientProfile(ClientProfileDto clientProfileData) { try { if (await ClientHasBusinessProfile(clientProfileData.ClientId)) { return(Guid.Empty); } var user = await _userManager.FindByEmailAsync(_userId); var clientProfile = _mapper.Map <ClientProfile>(clientProfileData); _dbContext.ClientProfiles.Add(clientProfile); await _dbContext.SaveChangesAsync(); return(clientProfile.Id); } catch (Exception) { return(Guid.Empty); } }
public async Task <OperationDetails> Create(ClientProfileDto clientProfileDto) { var user = await Database.UserManager.FindByEmailAsync(clientProfileDto.Email); if (user == null) { user = new ApplicationUser { Email = clientProfileDto.Email, UserName = clientProfileDto.Email }; var result = await Database.UserManager.CreateAsync(user, clientProfileDto.Password); if (result.Errors.Any()) { return(new OperationDetails(false, result.Errors.FirstOrDefault(), "")); } // Add role. await Database.UserManager.AddToRoleAsync(user.Id, clientProfileDto.Role); // Create customer profile. var clientProfile = new ClientProfile { Id = user.Id, Name = clientProfileDto.Name, Address = clientProfileDto.Address }; Database.ClientManager.Create(clientProfile); await Database.SaveAsync(); CurrentUser = new ClientProfileDto { Id = user.Id, }; return(new OperationDetails(true, "Регистрация успешно пройдена", "")); } return(new OperationDetails(false, "Пользователь с таким логином уже существует", "Email")); }
public async Task <IActionResult> Get(string clientId) { ClientProfileDto profile = await _clientProfileService.Get(clientId); return(Ok(profile)); }