コード例 #1
0
        public async Task <IActionResult> Profile()
        {
            var token = _httpContextAccessor.HttpContext.Session.GetString("JWToken");

            if (token == null || token == "")
            {
                TempData["NotLoggedin"] = "You must loggedIn ...";
                return(RedirectToAction("Index", "Account"));
            }

            OrderDto       model   = new OrderDto();
            AddressUserDto address = new AddressUserDto();

            using (var httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
                using (var response = await httpClient.GetAsync("https://localhost:5001/api/account/address"))
                {
                    string apiResponse = await response.Content.ReadAsStringAsync();

                    address = JsonConvert.DeserializeObject <AddressUserDto>(apiResponse);
                }
            }
            await PublicMethods();

            return(View("Profile", address));
        }
コード例 #2
0
        public async Task <ActionResult <AddressUserDto> > UpdateUserAddress(AddressUserDto address)
        {
            var user = await _userManager.FindByUserByClaimsPrincipleWithAddressAsync(HttpContext.User);

            user.AddressUser = _mapper.Map <AddressUserDto, AddressUser>(address);

            var result = await _userManager.UpdateAsync(user);

            if (result.Succeeded)
            {
                return(Ok(_mapper.Map <AddressUser, AddressUserDto>(user.AddressUser)));
            }

            return(BadRequest("Problem updating the user"));
        }
コード例 #3
0
        public async Task <IActionResult> UpdateAddress(AddressUserDto model)
        {
            var result = new AddressUserDto();

            try
            {
                var token = _httpContextAccessor.HttpContext.Session.GetString("JWToken");

                if (token == null || token == "")
                {
                    TempData["NotLoggedin"] = "You must loggedIn ...";
                    return(RedirectToAction("Index", "Account"));
                }

                using (var httpClient = new HttpClient())
                {
                    var myContent   = JsonConvert.SerializeObject(model);
                    var buffer      = System.Text.Encoding.UTF8.GetBytes(myContent);
                    var byteContent = new ByteArrayContent(buffer);
                    byteContent.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");

                    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
                    using (var response = await httpClient.PostAsync($"https://localhost:5001/api/account/updateaddress", byteContent))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            TempData["message"] = "Address updated succesfully!";
                            string apiResponse = await response.Content.ReadAsStringAsync();

                            result = JsonConvert.DeserializeObject <AddressUserDto>(apiResponse);
                        }
                        else
                        {
                            TempData["error"] = "Error!!! Address did not be updated!";
                        }
                    }
                }

                return(RedirectToAction(nameof(Profile)));
            }
            catch (Exception)
            {
                return(View());
            }
        }