public async Task <IHttpActionResult> ChangePassword(ForgotPasswordRequest model) { Responce <bool> responce = new Responce <bool>(); responce.Success = true; try { var user = await UserManager.FindByNameAsync(model.Email); if (user != null) { if (string.Equals(model.OTP, user.OTP) == true) { string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id); IdentityResult result = await UserManager.ResetPasswordAsync(user.Id, code, model.Password); //user.OTP = string.Empty; //await UserManager.UpdateAsync(user); if (!result.Succeeded) { List <string> errors = new List <string>(); foreach (var item in result.Errors) { errors.Add(item); } var Allerrors = string.Join(",", errors); responce.ResponeContent = false; responce.Fail(Allerrors); return(Content(HttpStatusCode.BadRequest, responce)); } responce.ResponeContent = true; responce.Success = true; return(Ok(responce)); } else { responce.Success = false; responce.ResponeContent = false; return(Content(HttpStatusCode.BadRequest, responce)); } } else { responce.Success = false; responce.ResponeContent = false; return(Content(HttpStatusCode.BadRequest, responce)); } } catch (Exception ex) { responce.Success = false; responce.ResponeContent = false; responce.Message = $"ERROR ChangePassword :{ex.ToString()}"; return(Content(HttpStatusCode.BadRequest, responce)); } }
public async Task <IHttpActionResult> UpdateCity(CityDTO citydto) { Responce <bool> responce = new Responce <bool>(); try { using (var db = new SSCEntities()) { var CheckCity = db.Citys.Find(citydto.Id); if (CheckCity == null) { responce.ResponeContent = false; responce.Success = false; responce.Message = "This city should not exist"; return(Content(HttpStatusCode.BadRequest, responce)); } var GetDefaultCityAdminUser = db.AspNetUsers.Where(a => a.IsDefaultAdmin == true && a.CityId == citydto.Id)?.FirstOrDefault(); if (GetDefaultCityAdminUser != null) { var appUser = await UserManager.FindByNameAsync(GetDefaultCityAdminUser.UserName); if (appUser != null) { if (appUser.UserName.ToLower() != citydto.AdminEmail.ToLower()) { appUser.UserName = citydto.AdminEmail; appUser.Email = citydto.AdminEmail; //appUser.City = citydto.CityName; //appUser.CityId = citydto.Id; IdentityResult result = await UserManager.UpdateAsync(appUser); if (!result.Succeeded) { var Errors = GetIdentityErrorResult(result); responce.Fail(Errors); responce.ResponeContent = false; return(Content(HttpStatusCode.BadRequest, responce)); } } } } DateTime dateTime = DateTime.UtcNow; Citys citys = CheckCity; citys.MCEmail = citydto.MCEmail; citys.FcciEmail = citydto.FcciEmail; citys.CountryId = citydto.CountryId ?? 1; citys.CityName = citydto.CityName; citys.IsActive = citydto.IsActive; citys.AdminEmail = citydto.AdminEmail; citys.ModifiedBy = citydto.ModifiedBy; citys.ModifiedDate = dateTime; db.Entry(citys).State = System.Data.Entity.EntityState.Modified; await db.SaveChangesAsync(); List <AspNetUsers> aspNetUsers = new List <AspNetUsers>(); aspNetUsers = db.AspNetUsers.Where(a => a.CityId == citys.Id).ToList(); foreach (var item in aspNetUsers) { item.City = citys.CityName; db.Entry(item).State = System.Data.Entity.EntityState.Modified; await db.SaveChangesAsync(); } responce.Success = true; responce.ResponeContent = true; } return(Ok(responce)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public async Task <IHttpActionResult> Register(RegisterRequest model) { Responce <RegistrationDTO> responce = new Responce <RegistrationDTO>(); int CityId = 0; responce.Success = true; RegistrationDTO registrationDTO = new RegistrationDTO(); if (!ModelState.IsValid) { var Errors = GetModelStateError(ModelState); responce.Fail(Errors); return(Content(HttpStatusCode.BadRequest, responce)); } SSCEntities db = new SSCEntities(); if (model.Cityid == 0) { var UserCity = db.Citys.Where(a => a.CityName.ToLower() == model.City.ToLower()).FirstOrDefault(); CityId = UserCity.Id; } else { CityId = model.Cityid; } //var UserCity = db.Citys.Where(a => a.CityName.ToLower() == model.City.ToLower()).FirstOrDefault(); var user = new ApplicationUser() { UserName = model.Email, Email = model.Email, City = model.City, PhoneNumber = model.PhoneNumber, Name = model.Name, CityId = CityId }; IdentityResult result = await UserManager.CreateAsync(user, model.Password); if (!result.Succeeded) { var Errors = GetIdentityErrorResult(result); responce.Fail(Errors); return(Content(HttpStatusCode.BadRequest, responce)); } else { IdentityResult assignroleresult = await UserManager.AddToRoleAsync(user.Id, "Volunteer"); if (!assignroleresult.Succeeded) { var Errors = GetIdentityErrorResult(assignroleresult); responce.Fail(Errors); return(Content(HttpStatusCode.BadRequest, responce)); } else { registrationDTO.Name = user.Name; registrationDTO.Email = user.Email; registrationDTO.City = user.City; registrationDTO.PhoneNumber = user.PhoneNumber; registrationDTO.Password = model.Password; responce.ResponeContent = registrationDTO; } } return(Ok(responce)); }
public async Task <IHttpActionResult> CreateCity(CityDTO citydto) { Responce <bool> responce = new Responce <bool>(); try { var IsUserExist = await UserManager.FindByNameAsync(citydto.AdminEmail); if (IsUserExist != null) { var Errors = "User email already registered."; responce.Fail(Errors); responce.ResponeContent = false; return(Content(HttpStatusCode.BadRequest, responce)); } using (var db = new SSCEntities()) { var CheckCity = db.Citys.Where(a => a.CityName.ToLower() == citydto.CityName.ToLower())?.ToList() ?? new List <Citys>(); if (CheckCity.Count > 0) { responce.ResponeContent = false; responce.Success = false; responce.Message = "City already added"; return(Content(HttpStatusCode.BadRequest, responce)); } DateTime dateTime = DateTime.UtcNow; Citys citys = new Citys(); citys.MCEmail = citydto.MCEmail; citys.FcciEmail = citydto.FcciEmail; citys.CityName = citydto.CityName; citys.IsActive = citydto.IsActive; citys.AdminEmail = citydto.AdminEmail; citys.CreatedBy = citydto.CreatedBy; citys.CreatedDate = dateTime; citys.ModifiedBy = citydto.ModifiedBy; citys.ModifiedDate = dateTime; db.Entry(citys).State = System.Data.Entity.EntityState.Added; await db.SaveChangesAsync(); var lows = db.Lows.ToList(); SSCEntities sscEntities = new SSCEntities(); foreach (var item in lows) { CityLows cityLows = new CityLows(); cityLows.Id = citys.Id; cityLows.LowId = item.Id; sscEntities.Entry(cityLows).State = System.Data.Entity.EntityState.Added; await sscEntities.SaveChangesAsync(); } var user = new ApplicationUser() { UserName = citydto.AdminEmail, Email = citydto.AdminEmail, City = citydto.CityName, PhoneNumber = citydto.MobileNo, Name = citydto.UserName, IsDefaultAdmin = true, CityId = citys.Id }; IdentityResult result = await UserManager.CreateAsync(user, citydto.Password); if (!result.Succeeded) { var Errors = GetIdentityErrorResult(result); responce.Fail(Errors); responce.ResponeContent = false; return(Content(HttpStatusCode.BadRequest, responce)); } db.UpdateUserRole("989D2937-0FC5-497D-980C-B2E8583602B9", user.Id); responce.ResponeContent = true; responce.Success = true; } return(Ok(responce)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }