public string RegisterUser(ValidateUserDTO validateUser) { User newUser = new User(); var outputMessage = "Successfully Registered"; try { var userExists = db.Users.Where(e => e.EmpID == validateUser.EmpId).FirstOrDefault(); if (userExists == null) { newUser = new User() { FirstName = validateUser.FirstName, LastName = validateUser.LastName, EmailId = validateUser.EmailId, UserId = Convert.ToInt64(validateUser.EmpId), EmpID = validateUser.EmpId.PadLeft(5, '0'), CreatedOn = DateTime.Now.ToLocalTime() }; db.Users.Add(newUser); db.SaveChanges(); } else { outputMessage = "User Exists"; } } catch (Exception ex) { outputMessage = "Registration Failed."; } return(outputMessage); }
//public string ValidateUser() public ValidateUserDTO GetValidateUser(string UserName, string Password) { ValidateUserDTO retObject = new ValidateUserDTO(); List <ValidateUserDTO> lstValidateUser = new List <ValidateUserDTO>(); try { UserName = "******" + UserName; using (PrincipalContext pCtx = new PrincipalContext(ContextType.Domain, "192.168.192.12", UserName, Password)) { GroupPrincipal group = GroupPrincipal.FindByIdentity(pCtx, "USERS"); var user = UserPrincipal.FindByIdentity(pCtx, UserName); retObject.FirstName = user.GivenName; retObject.LastName = user.Surname; retObject.Name = user.DisplayName; retObject.EmpId = user.EmployeeId; retObject.EmailId = user.SamAccountName + "@hitachiconsulting.com"; lstValidateUser.Add(retObject); } } catch (Exception ex) { string output = "Message: " + ex.Message + Environment.NewLine + "Stack Trace: " + ex.StackTrace; } return(retObject); }
public ActionResult Login(string txtUserName, string txtPassword) { // Lets first check if the Model is valid or not using (EventAppDataModelEntity entities = new EventAppDataModelEntity()) { try { ValidateUserDTO retObject = new ValidateUserDTO(); string UserName = "******" + txtUserName; using (PrincipalContext pCtx = new PrincipalContext(ContextType.Domain, "0.0.0.0", UserName, txtPassword)) { FormsAuthentication.SetAuthCookie(UserName, false); GroupPrincipal group = GroupPrincipal.FindByIdentity(pCtx, "USERS"); var user = UserPrincipal.FindByIdentity(pCtx, UserName); retObject.FirstName = user.GivenName; retObject.LastName = user.Surname; retObject.Name = user.DisplayName; retObject.EmpId = user.EmployeeId; retObject.EmailId = user.SamAccountName + "@doaminname.com"; Session["Username"] = user.DisplayName; return(RedirectToAction("SendPushNotification", "PushNotification")); } } catch (Exception ex) { //string output = "Message: " + ex.Message + Environment.NewLine + "Stack Trace: " + ex.StackTrace; ViewBag.Message = "Invalid User Credentials"; return(View()); } } }
public ValidateUserDTO Update(UserUpdateDTO user) { var result = new ValidateUserDTO() { }; if (this.userRepository.Exists(user.Login, user.Id)) { var errors = new List <string>() { "The given login '" + user.Login + "' is already in use." }; result.OverallErrors = errors; return(result); } var applicationUser = this.userRepository.Get(user.Id); applicationUser.Name = user.Name; applicationUser.Login = user.Login; applicationUser.Password = PasswordEncryptor.Encrypt(user.Password); this.userRepository.Update(applicationUser); result.ValidatedUser = this.Get(user.Id); return(result); }
public ValidateUserDTO Save(UserSaveDTO user) { var result = new ValidateUserDTO() { }; if (this.userRepository.Exists(user.Login, null)) { var errors = new List <string>() { "The given login '" + user.Login + "' is already in use." }; result.OverallErrors = errors; return(result); } var applicationUser = Mapper.Map <ApplicationUser>(user); applicationUser.Password = PasswordEncryptor.Encrypt(user.Password); applicationUser.Roles = new ApplicationUserRole[] { new ApplicationUserRole() { Name = "User" } }; result.ValidatedUser = Mapper.Map <UserDTO>(this.userRepository.Create(applicationUser)); return(result); }
public async Task <bool> ValidateUsername(ValidateUserDTO user) { if (user.Id != 0) { return(await _context.Users.AnyAsync(x => x.Username.ToLower() == user.Username.ToLower() && x.Id != user.Id)); } else { return(await _context.Users.AnyAsync(x => x.Username.ToLower() == user.Username.ToLower())); } }
public async Task <ActionResult> LoginAsync([FromBody] ValidateUserDTO validateUserDTO) { var user = await _userService.ValidateUser(validateUserDTO.Email, validateUserDTO.Password); if (user == null) { return(Unauthorized("invalid email or password")); } return(Ok(new { token = GenerateToken(user) })); }
public async Task <IActionResult> ValidateUserAsync([FromBody] ValidateUserDTO validateUserDTO) { if (validateUserDTO == null || string.IsNullOrEmpty(validateUserDTO.Email) || string.IsNullOrEmpty(validateUserDTO.Password)) { return(BadRequest()); } var user = await _userService.ValidateUserAsync(validateUserDTO.Email, validateUserDTO.Password); if (user == null) { return(Unauthorized("Email or password is wrong")); } //need to change to the newly created method return(Ok(new { // creating the instance of the token token = GenerateToken(user) })); }
public async Task <bool> ValidateUsername([FromBody] ValidateUserDTO user) { return(await _userService.ValidateUsername(user)); }