public async Task <IActionResult> OnPostAsync() { var user = await _userManager.GetUserAsync(User); if (user == null) { return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.")); } if (!ModelState.IsValid) { await LoadAsync(user); return(Page()); } var phoneNumber = await _userManager.GetPhoneNumberAsync(user); if (Input.PhoneNumber != phoneNumber) { var setPhoneResult = await _userManager.SetPhoneNumberAsync(user, Input.PhoneNumber); if (!setPhoneResult.Succeeded) { StatusMessage = "Unexpected error when trying to set phone number."; return(RedirectToPage()); } } //User info region var applicationUserInfo = new ApplicationUserInfo { AddressLine1 = Input.AddressLine1, AddressLine2 = Input.AddressLine2, Country = Input.Country, City = Input.City, User = user }; if ((await _userInfoRepository.GetById(user.UserInfoId)) == null) { await _userInfoRepository.Add(applicationUserInfo); } else { _userInfoRepository.Update(applicationUserInfo); } await _userInfoRepository.SaveChangesAsync(); // await _signInManager.RefreshSignInAsync(user); StatusMessage = "Your profile has been updated"; return(RedirectToPage()); }
/// <summary> /// Get signed user information data (login and token) /// </summary> private ApplicationUserInfo GetSignedUserInformation() { ApplicationUserInfo result = null; try { string user = _accessor?.HttpContext?.User?.Claims?.FirstOrDefault(f => f.Type.EndsWith("nameidentifier"))?.Value; string token = _accessor?.HttpContext?.Request?.Headers?.FirstOrDefault(f => f.Key == "Authorization").Value.FirstOrDefault(x => x.ToLower().StartsWith("bearer")); if (!string.IsNullOrWhiteSpace(user) && !string.IsNullOrWhiteSpace(token)) { result = new ApplicationUserInfo(user, token); } } catch { /* Not to do. Fire and forget */ } return(result); }
private ApplicationUserInfo GetOrCreateApplicationUserInfo() { var httpContext = _httpContextAccessor.HttpContext; var applicationUserInfo = (ApplicationUserInfo)httpContext.Items[nameof(ApplicationUserInfo)]; if (applicationUserInfo == null) { long?userId = null; if (httpContext.User.Identity.IsAuthenticated) { var userIdentity = httpContext.User.Identities.First(); var userIdString = userIdentity.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value; userId = long.Parse(userIdString); } httpContext.Items[nameof(ApplicationUserInfo)] = applicationUserInfo = new ApplicationUserInfo { UserId = userId }; } return(applicationUserInfo); }
/*USABLE METHODS*/ /* * Saving course file (image from local computer or by url) * file - file from users local PC * fileSite - url of image * course - course to save image * serverMapPath - path on server to save image: * for example: * serverMapPAth - C://aaaa//aaa// * image new path - C://aaaa//aaa//Course{ID}//Course{ID}.extention */ public static Boolean SavePersonalFile(HttpPostedFileBase file, String fileSite, ApplicationUserInfo appuser, String serverMapPath, String fileNameBegin = "AppUser", Boolean save = true) { if (file != null) { String fileName = fileNameBegin + appuser.Id.ToString(); String imagePath = SaveImageToFolder(fileName, file, serverMapPath); if (imagePath != String.Empty) { appuser.Image = imagePath; return true; } } ///*saving images http else if (fileSite != String.Empty) { if (UserHelper.ValidateHttpImageRoute(fileSite)) { appuser.Image = fileSite; return true; } } else if (save) { appuser.Image = defaultImagePath; return true; } /*saving image is not main task*/ if (!save) return true; return false; }