public async Task <IHttpActionResult> CreateNewUser(MediaUserModel model) { var response = new RequestStatus(); var passwd = CodeGenerator.ConfirmationString(6); var user = new ApplicationUser { ConfirmationCode = string.Empty, EmailConfirmed = true, UserName = model.Email, Email = model.Email, Name = model.Name }; //, ProfileImage = model.ImageUrl }; IdentityResult result = await UserManager.CreateAsync(user, passwd); string[] roles = model.UserRoles.Split(','); if (!result.Succeeded) { return(GetErrorResult(result)); } else { foreach (string role in roles) { await UserManager.AddToRoleAsync(user.Id, role); } var message = new IdentityMessage { Destination = model.Email, Body = string.Format("Dear <b>{0}</b>,<br/><br/>Your account has been created in KE Automated Communications System tool.<br/><br/>Use the following information to log in the system: <br/><br/>Website url: <b>{1}</b><br/>UserName: <b>{2}</b><br/>Password: <b>{3}</b><br/><br/>Once logged in the system, you are allowed to change your password.<br/><br/> Sincerely,<br/>Automated Communications Team", user.Name, baseUrl, model.Email, passwd), Subject = "KE Automated Communications System- Invitation to Access" }; await UserManager.EmailService.SendAsync(message); response.StatusCode = "0"; response.Message = "Success"; return(Ok(response)); } }
public async Task <IHttpActionResult> UpdateUser(MediaUserModel model) { var response = new RequestStatus(); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = await UserManager.FindByEmailAsync(model.Email); string[] roles = model.UserRoles.Split(','); if (user == null) { response.StatusCode = "1"; response.Message = "User not Found."; return(BadRequest("Invalid User")); } else { //User is Authenticated! user.Name = model.Name; user.Roles.Clear(); foreach (string role in roles) { await UserManager.AddToRoleAsync(user.Id, role); } await UserManager.UpdateAsync(user); response.StatusCode = "0"; response.Message = "Success"; return(Ok(response)); } }
public async Task <IHttpActionResult> DeleteUser(MediaUserModel model) { var response = new RequestStatus(); ApplicationDbContext context = new ApplicationDbContext(); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = await UserManager.FindByEmailAsync(model.Email); var resp = await userRepository.DeleteUserReference(user.Id); if (resp) { await UserManager.DeleteAsync(user); } response.StatusCode = "0"; response.Message = "Success"; return(Ok(response)); }