public JsonResult Put([FromBody] User user) { string msg = "Action: Edit" + "; UserId: " + user.Id; bool status = false; UserServiceResult result = null; try { result = this.GetService <UserService>().SignUp(user); status = true; } catch (Exception ex) { msg += "; Error:" + ex.Message; } if (result.Status == Result.Failure) { msg += "; Error:" + result.Message; } object json = new { status = status, msg = msg }; return(Json(json)); }
public UserServiceResult LogIn(string id, string pwd) { UserServiceResult result = new UserServiceResult(); User user = Get(id); if (user == null) { result.Status = Result.Failure; result.Message = INVALID_USERNAME; } else { if (user.Password.Equals(pwd)) { result.Status = Result.Success; result.Type = (int)user.UserType; result.User = user; } else { result.Status = Result.Failure; result.Message = INVALID_PWD; } } return(result); }
public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { UserServiceResult result = await UserService.Register(model.UserName, model.Email, model.Password); if (result == UserServiceResult.error) { ModelState.AddModelError("Error", "An error occured, please try again"); return(View(model)); } else if (result == UserServiceResult.userAlreadyExists) { ModelState.AddModelError("Email", "Such user already exist"); return(View(model)); } else if (result == UserServiceResult.success) { string userId = UserService.GetUserIdByEmail(model.Email); string confirmationEmailLink = GetConfirmationLink(userId, model.Email); UserService.SendConfirmationEmail(model.UserName, model.Email, confirmationEmailLink); return(RedirectToAction("ConfirmationSent", "Account", new ResendConfirmationEmailViewModel { Email = model.Email })); } } return(View(model)); }
public ActionResult ConfirmEmail(string token, string userId, string email) { UserServiceResult result = UserService.ConfirmEmail(userId, token); if (result == UserServiceResult.emailAlreadyConfirmed) { ModelState.AddModelError("Error", "Email already confirmed"); return(View(new ResendConfirmationEmailViewModel { Email = "" })); } if (result == UserServiceResult.userNotExist) { ModelState.AddModelError("Error", "User not exists"); return(View(new ResendConfirmationEmailViewModel { Email = "" })); } ModelState.AddModelError("Error", $"Email {email} was successfuly confirmed."); return(View(new ResendConfirmationEmailViewModel { Email = email })); }
public UserServiceResult SignUp(User user) { UserServiceResult result = new UserServiceResult(); User dbUser = Get(user.Id); if (dbUser == null) { Create(user); result.User = user; result.Status = Result.Success; } else { result.Status = Result.Failure; result.Message = USER_ALREADY_EXIST; } return(result); }
public ActionResult ResendConfirmation(ResendConfirmationEmailViewModel model) { if (ModelState.IsValid) { string userId = UserService.GetUserIdByEmail(model.Email); if (userId == null) { ModelState.AddModelError("Error", "There is no user with such email"); } else { string confirmationEmailLink = GetConfirmationLink(userId, model.Email); string userName = UserService.GetUserNameByEmail(model.Email); UserServiceResult result = UserService.SendConfirmationEmail(userName, model.Email, confirmationEmailLink); if (result == UserServiceResult.success) { return(RedirectToAction("ConfirmationSent", "Account", new ResendConfirmationEmailViewModel { Email = model.Email })); } if (result == UserServiceResult.emailNotSent) { ModelState.AddModelError("Error", "Email not sent"); } if (result == UserServiceResult.emailAlreadyConfirmed) { ModelState.AddModelError("Error", "Email already confirmed"); } } } ModelState.AddModelError("Error", "Something went wrong"); return(View(model)); }
public ActionResult Login(LoginViewModel model) { if (ModelState.IsValid) { UserServiceResult result = UserService.IsUserEmailConfirmed(model.Email); if (result == UserServiceResult.userNotExist) { ModelState.AddModelError("Error", "User not exists"); return(View(model)); } if (result == UserServiceResult.emailNotConfirmed) { ModelState.AddModelError("Error", "Your email not confirmed"); return(View(model)); } ClaimsIdentity claim = UserService.PasswordEmailSignIn(model.Email, model.Password); if (claim == null) { ModelState.AddModelError("Error", "Invalid email or password"); } else { AuthenticationManager.SignOut(); AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = model.RememberMe, }, claim); return(RedirectToAction("AllProjects", "Project")); } } return(View(model)); }
public async Task <UserServiceResult> LoginAsync(string nameOrEmail, string password) { User user = await userManager.FindUserByNameOrEmailAsync(nameOrEmail); if (user == null || !await userManager.CheckPasswordAsync(user, password)) { return(UserServiceResult.BadResult( ErrorView.SoftError("UsernamePasswordInvalid", "The username or password is invalid."))); } if (!await userManager.IsEmailConfirmedAsync(user)) { return(UserServiceResult.BadResult( ErrorView.SoftError("EmailNotConfirmed", "You must have a confirmed email to log in."))); } if (await userManager.IsUserInRoleAsync(user.Id, RoleNames.Banned)) { return(UserServiceResult.BadResult(new ErrorView("UserBanned", "User is banned"))); } return(UserServiceResult.OkResult(user)); }