private async void InsertClaimRoles(UserCustomerModel user, UsuarioSistema users, Microsoft.AspNetCore.Http.IFormCollection collection) { //create relacao var chks = (from itens in collection.Keys where itens.Contains("chk_") select itens.Replace("chk_", string.Empty)).ToList(); var lstGrupoAcesso = TempData["lstGrupoAcesso"] == null?GetListGrupoAcesso().Result : (List <GruposAcesso>)TempData["lstGrupoAcesso"]; foreach (var item in chks) { var useracesso = new UsuarioGruposAcesso(); useracesso.IdGrupoAcessos = Convert.ToInt64(item); useracesso.IdUsuarioSistema = users.Id; _context.Add(useracesso); //salva na claims var itemAcesso = lstGrupoAcesso.Find(p => p.Id == Convert.ToInt64(item.ToString())); if (itemAcesso != null) { await _userManager.AddClaimAsync(user, new Claim(itemAcesso.PageAcesso, ConstantPermissions.todasPermission, itemAcesso.PageAcesso, users.GuidId)); } } }
private async Task <UserCustomerModel> InsertIdentity(Microsoft.AspNetCore.Http.IFormCollection collection, UsuarioSistema users) { string caminhoDestinoArquivoOriginal = await Uploadfile(collection, users.Nome); if (!string.IsNullOrEmpty(caminhoDestinoArquivoOriginal)) { users.PathPhoto = caminhoDestinoArquivoOriginal; } //add user no aspnetuser var user = new UserCustomerModel { UserName = users.Nome, Email = users.Email, EmailConfirmed = true, PhotoIdPath = users.PathPhoto, PhoneNumber = users.Telefone, }; var result = await _userManager.CreateAsync(user, users.Password); if (!result.Succeeded) { foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } return(user); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null, IFormFile photouser = null) { returnUrl = returnUrl ?? Url.Content("~/"); ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); if (ModelState.IsValid) { var caminhoFoto = photouser != null ? photouser.FileName : Input.PhotoPath != null ? Input.PhotoPath.FileName : null; string nomeArquivo = string.Concat("foto_", Input.UserName, DateTime.Now.Millisecond.ToString(), ".png"); if (!string.IsNullOrEmpty(caminhoFoto)) { string caminhoDestinoArquivoOriginal = string.Concat( _appEnvironment.WebRootPath, _config.GetValue <string>("FolderImg"), nomeArquivo); //ajustar using (var stream = new FileStream(caminhoDestinoArquivoOriginal, FileMode.Create)) { await photouser.CopyToAsync(stream); } } var user = new UserCustomerModel { UserName = Input.UserName, Email = Input.Email, EmailConfirmed = true, PhotoIdPath = nomeArquivo, PhoneNumber = Input.PhoneNumber }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { _logger.LogInformation("Usuário criado com sucesso."); return(LocalRedirect(returnUrl)); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }
public async Task <HttpResponseMessage> UploadCustomerProfile() { // Check if the request contains multipart/form-data. if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } string root = HttpContext.Current.Server.MapPath("~/CustomerPhoto/temp"); CustomMultipartFormDataStreamProvider1 provider = new CustomMultipartFormDataStreamProvider1(root); try { StringBuilder sb = new StringBuilder(); // Holds the response body Type type = typeof(CustomerModel); // Get type pointer CustomerModel locationImagesModel = new CustomerModel(); // Read the form data and return an async task. CustomMultipartFormDataStreamProvider1 x = await Request.Content.ReadAsMultipartAsync(provider); int CustomerId = 0; string Bio = ""; string Privacy = ""; int Age = 0; string Gender = ""; string Music = ""; string Photography = ""; string Camping = ""; string Hiking = ""; // This illustrates how to get the form data. foreach (var key in provider.FormData.AllKeys) { if (key == "CustomerId") { string propertyValue = provider.FormData.GetValues(key).FirstOrDefault(); if (propertyValue != null) { CustomerId = Convert.ToInt32(propertyValue); if (CustomerId == 0) { return(ErrorMessage("error", "Customer Id is blank.")); } } } if (key == "Age") { string propertyValue = provider.FormData.GetValues(key).FirstOrDefault(); if (propertyValue != null) { Age = Convert.ToInt32(propertyValue); if (Age == 0) { return(ErrorMessage("error", "Age is blank.")); } } } if (key == "Bio") { string propertyValue = provider.FormData.GetValues(key).FirstOrDefault(); if (propertyValue != null) { Bio = propertyValue; if (Bio == "" || Bio == null) { return(ErrorMessage("error", "Bio is blank.")); } } } if (key == "Privacy") { string propertyValue = provider.FormData.GetValues(key).FirstOrDefault(); if (propertyValue != null) { Privacy = propertyValue; if (Privacy != EnumValue.GetEnumDescription(EnumValue.Privacy.Public) && Privacy != EnumValue.GetEnumDescription(EnumValue.Privacy.Private)) { return(ErrorMessage("error", "Privacy is wrong.")); } if (Privacy == "" || Privacy == null) { return(ErrorMessage("error", "Privacy is blank.")); } } } if (key == "Gender") { string propertyValue = provider.FormData.GetValues(key).FirstOrDefault(); if (propertyValue != null) { Gender = propertyValue; if (Gender != EnumValue.GetEnumDescription(EnumValue.Gender.Female) && Gender != EnumValue.GetEnumDescription(EnumValue.Gender.Male)) { return(ErrorMessage("error", "Gender is wrong.")); } if (Gender == "" || Gender == null) { return(ErrorMessage("error", "Gender is blank.")); } } } if (key == "Music") { string propertyValue = provider.FormData.GetValues(key).FirstOrDefault(); if (propertyValue != null) { Music = propertyValue; if (Music == "" || Music == null) { return(ErrorMessage("error", "Music is blank.")); } } } if (key == "Photography") { string propertyValue = provider.FormData.GetValues(key).FirstOrDefault(); if (Photography != null) { Photography = propertyValue; if (Photography == "" || Photography == null) { return(ErrorMessage("error", "Photography is blank.")); } } } if (key == "Camping") { string propertyValue = provider.FormData.GetValues(key).FirstOrDefault(); if (propertyValue != null) { Camping = propertyValue; if (Camping == "" || Camping == null) { return(ErrorMessage("error", "Camping is blank.")); } } } if (key == "Hiking") { string propertyValue = provider.FormData.GetValues(key).FirstOrDefault(); if (propertyValue != null) { Hiking = propertyValue; if (Hiking == "" || Hiking == null) { return(ErrorMessage("error", "Hiking is blank.")); } } } if (CustomerId != 0 && Bio != "" && Privacy != "" && Age != 0 && Gender != "" && Music != "" && Photography != "" && Camping != "" && Hiking != "") { break; } } //Delete all already exist files HomeHelp.Entity.Customer Customer = _CustomerService.GetCustomers().Where(c => c.CustomerId == CustomerId && c.IsActive == true).FirstOrDefault(); if (Customer != null) { if (Customer.PhotoPath != "" && Customer.PhotoPath != null) { DeleteImage(Customer.BackgroundImage); } } else { return(Request.CreateResponse(HttpStatusCode.OK, CommonCls.CreateMessage("error", "No user found."), Configuration.Formatters.JsonFormatter)); } // Process the list of files found in the directory. string[] fileEntries = Directory.GetFiles(root); foreach (string fileName in fileEntries) { var fileFound = provider.FileData.Where(c => c.Headers.ContentDisposition.FileName.Replace("\"", string.Empty) == Path.GetFileName(fileName)).FirstOrDefault(); if (fileFound != null) { //string NewFileName = Guid.NewGuid() + "_Event" + EventId.ToString() + Path.GetExtension(fileName); string NewFileName = Guid.NewGuid() + Path.GetExtension(fileName); string NewRoot = HttpContext.Current.Server.MapPath("~/CustomerPhoto") + "\\" + NewFileName; System.IO.File.Move(fileName, NewRoot); string URL = CommonCls.GetURL() + "/CustomerPhoto/" + NewFileName; Customer.PhotoPath = URL; sb.Append(URL); } } Customer.Bio = Bio; Customer.Privacy = Privacy; Customer.Age = Age; Customer.Gender = Gender; _CustomerService.UpdateCustomer(Customer); HobbiesAndInterests hobbies = _HobbiesAndInterestsService.GetHobbiesAndInterestss().Where(c => c.CustomerId == CustomerId).FirstOrDefault(); if (hobbies != null) { hobbies.Music = Convert.ToInt32(Music); hobbies.Hiking = Convert.ToInt32(Hiking); hobbies.Camping = Convert.ToInt32(Camping); hobbies.Photography = Convert.ToInt32(Photography); hobbies.CustomerId = CustomerId; _HobbiesAndInterestsService.UpdateHobbiesAndInterests(hobbies); } else { HobbiesAndInterests objHobbies = new HobbiesAndInterests(); objHobbies.Music = Convert.ToInt32(Music); objHobbies.Hiking = Convert.ToInt32(Hiking); objHobbies.Camping = Convert.ToInt32(Camping); objHobbies.Photography = Convert.ToInt32(Photography); objHobbies.CustomerId = CustomerId; _HobbiesAndInterestsService.InsertHobbiesAndInterests(objHobbies); } Mapper.CreateMap <Customer, UserCustomerModel>(); UserCustomerModel userCustomerModel = Mapper.Map <Customer, UserCustomerModel>(Customer); userCustomerModel.Music = Music; userCustomerModel.Hiking = Hiking; userCustomerModel.Camping = Camping; userCustomerModel.Photography = Photography; return(ErrorMessage("success", userCustomerModel)); } catch (System.Exception e) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e)); } }