public async Task <OperationDetails> Register(RegisterModel model) { await SetInitialDataAsync(); if (ModelState.IsValid) { AnimeUserModel animeUserModel = new AnimeUserModel { Email = model.Email, Password = model.Password, Address = model.Address, Name = model.Name, Role = "user" }; OperationDetails operationDetails = await UserService.Create(animeUserModel); if (operationDetails.Succedeed) { return(new OperationDetails(true, "Successful Registration")); } else { ModelState.AddModelError(operationDetails.Message, operationDetails.Message); } } return(new OperationDetails(false, "Model state is not valid")); }
public async Task <OperationDetails> Login(LoginModel model) { await SetInitialDataAsync(); if (ModelState.IsValid) { AnimeUserModel animeUserModel = new AnimeUserModel { Email = model.Email, Password = model.Password }; ClaimsIdentity claim = await UserService.Authenticate(animeUserModel); if (claim == null) { ModelState.AddModelError("", "Wrong login or password."); } else { AuthenticationManager.SignOut(); AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = true }, claim); return(new OperationDetails(true, "You've been successfully logged in")); } } return(new OperationDetails(false, "Model state is not valid")); }
public async Task <OperationDetails> Create(AnimeUserModel userModel) { AnimeUser user = await userManager.FindByEmailAsync(userModel.Email); if (user == null) { user = new AnimeUser { Email = userModel.Email, UserName = userModel.Email }; var result = await userManager.CreateAsync(user, userModel.Password); if (result.Errors.Count() > 0) { return(new OperationDetails(false, result.Errors.FirstOrDefault())); } await userManager.AddToRoleAsync(user.Id, userModel.Role); ClientProfile clientProfile = new ClientProfile { Id = user.Id, Address = userModel.Address, Name = userModel.Name }; clientManager.Create(clientProfile); await repo.SaveAsync(); return(new OperationDetails(true, "Регистрация успешно пройдена")); } else { return(new OperationDetails(false, "Пользователь с таким логином уже существует")); } }
public async Task <ClaimsIdentity> Authenticate(AnimeUserModel userModel) { ClaimsIdentity claim = null; AnimeUser user = await userManager.FindAsync(userModel.Email, userModel.Password); if (user != null) { claim = await userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); } return(claim); }
public async Task SetInitialData(AnimeUserModel adminAnime, List <string> roles) { foreach (string roleName in roles) { var role = await roleManager.FindByNameAsync(roleName); if (role == null) { role = new AnimeRole { Name = roleName }; await roleManager.CreateAsync(role); } } await Create(adminAnime); }