public async Task <ActionResult> UnLock(UserForCRUD userToUnLock)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    HttpResponseMessage response = await _accountService.UnLockUser(userToUnLock);

                    var responseData = response.Content.ReadAsStringAsync().Result;
                    if (response.IsSuccessStatusCode)
                    {
                        // UnLock User successfully
                        return(RedirectToAction("Index"));
                    }

                    // UnLock User failed
                    ResponseObj resObj = JsonConvert.DeserializeObject <ResponseObj>(responseData);

                    ModelState.Clear();
                    ModelState.AddModelError("", "Error in Unlocking User! Reason: " + resObj.Message);
                }
                else
                {
                    ModelState.Clear();
                    ModelState.AddModelError("", $"Error in Unlocking User on form!");
                }

                return(View(userToUnLock));
            }
            catch (Exception ex)
            {
                object cauThongBao = $"Error in UnLocking User!<br /> Reason: {ex.Message}";
                return(View("Error", cauThongBao));
            }
        }
Exemple #2
0
        public async Task <HttpResponseMessage> UnLockUser(UserForCRUD userToLock)
        {
            using (var client = new HttpClient())
            {
                string requestParams = "api/account/" + userToLock.Id + "/unlock";

                client.BaseAddress = new Uri(SD.BaseUrl);
                client.DefaultRequestHeaders.Clear();

                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 |
                                                       SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

                // Access token
                string accessToken = _userSession.AccessToken;

                var request = new HttpRequestMessage(HttpMethod.Post, requestParams);
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                request.Content = new StringContent(JsonConvert.SerializeObject(userToLock));
                request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                HttpResponseMessage response = await client.SendAsync(request);

                return(response);
            }
        }
        public async Task <ActionResult> Edit(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return(RedirectToAction("Index"));
            }

            try
            {
                UserForCRUD         userFromDb = new UserForCRUD();
                HttpResponseMessage response   = await _accountService.GetUser(id);

                var responseData = response.Content.ReadAsStringAsync().Result;
                if (!response.IsSuccessStatusCode)
                {
                    return(View("Error", $"Error in finding User to edit!<br />"));
                }
                else
                {
                    userFromDb = JsonConvert.DeserializeObject <UserForCRUD>(responseData);
                    if (userFromDb == null)
                    {
                        return(View("Error", $"Cannot find User with Id (" + id + ")."));
                    }

                    // Found User
                    userFromDb.StringLockoutEnd = !userFromDb.LockoutEnd.HasValue ? "" : userFromDb.LockoutEnd.Value.ToString("dd/MM/yyyy hh:mm:ss tt");
                }

                return(View(userFromDb));
            }
            catch (Exception ex)
            {
                object cauThongBao = $"Error in editing User!<br /> Reason: {ex.Message}";
                return(View("Error", cauThongBao));
            }
        }
        public async Task <ActionResult> Lock(UserForCRUD userToLock)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // Convert to Date Time for Lockout End
                    if (!string.IsNullOrEmpty(userToLock.StringLockoutEnd))
                    {
                        string[] dateAndTime = userToLock.StringLockoutEnd.Split(' '); // get dd/MM/yyyy hh:mm:ss AM/PM
                        string[] dmy         = dateAndTime[0].Split('/');              // get dd/MM/yyyy
                        string[] hms         = dateAndTime[1].Split(':');              // get hh:mm:ss

                        int dd = Convert.ToInt32(dmy[0]);
                        int mm = Convert.ToInt32(dmy[1]);
                        int yy = Convert.ToInt32(dmy[2]);

                        int hh = Convert.ToInt32(hms[0]);
                        int mi = Convert.ToInt32(hms[1]);
                        int ss = Convert.ToInt32(hms[2]);

                        if (dateAndTime[2] == "PM")
                        {
                            hh = hh + 12;
                        }

                        DateTime dt = new DateTime(yy, mm, dd, hh, mi, ss);
                        userToLock.LockoutEnd = dt;
                    }
                    else
                    {
                        userToLock.LockoutEnd = DateTime.Now.AddYears(100);
                    }

                    HttpResponseMessage response = await _accountService.LockUser(userToLock);

                    var responseData = response.Content.ReadAsStringAsync().Result;
                    if (response.IsSuccessStatusCode)
                    {
                        // Lock User successfully
                        return(RedirectToAction("Index"));
                    }

                    // Lock User failed
                    ResponseObj resObj = JsonConvert.DeserializeObject <ResponseObj>(responseData);

                    ModelState.Clear();
                    ModelState.AddModelError("", "Error in locking User! Reason: " + resObj.Message);
                }
                else
                {
                    ModelState.Clear();
                    ModelState.AddModelError("", $"Error in locking User on form!");
                }

                return(View(userToLock));
            }
            catch (Exception ex)
            {
                object cauThongBao = $"Error in Locking User!<br /> Reason: {ex.Message}";
                return(View("Error", cauThongBao));
            }
        }
        public async Task <ActionResult> Edit(UserForCRUD userToUpdate)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // Convert to Date Time for Lockout End
                    if (!string.IsNullOrEmpty(userToUpdate.StringLockoutEnd))
                    {
                        string[] dateAndTime = userToUpdate.StringLockoutEnd.Split(' '); // get dd/MM/yyyy hh:mm:ss AM/PM
                        string[] dmy         = dateAndTime[0].Split('/');                // get dd/MM/yyyy
                        string[] hms         = dateAndTime[1].Split(':');                // get hh:mm:ss

                        int dd = Convert.ToInt32(dmy[0]);
                        int mm = Convert.ToInt32(dmy[1]);
                        int yy = Convert.ToInt32(dmy[2]);

                        int hh = Convert.ToInt32(hms[0]);
                        int mi = Convert.ToInt32(hms[1]);
                        int ss = Convert.ToInt32(hms[2]);

                        if (dateAndTime[2] == "PM")
                        {
                            hh = hh + 12;
                        }

                        DateTime dt = new DateTime(yy, mm, dd, hh, mi, ss);
                        userToUpdate.LockoutEnd = dt;
                    }

                    using (var client = new HttpClient())
                    {
                        client.BaseAddress = new Uri(SD.BaseUrl);

                        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 |
                                                               SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

                        client.DefaultRequestHeaders.Clear();

                        // Access token
                        string accessToken = "";
                        accessToken = _userSession.AccessToken;

                        // Edit User
                        string requestParams = SD.BaseUrl + "api/account/" + userToUpdate.Id + "/update";
                        var    request       = new HttpRequestMessage(HttpMethod.Put, requestParams);
                        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                        request.Content = new StringContent(JsonConvert.SerializeObject(userToUpdate));
                        request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                        HttpResponseMessage response = await client.SendAsync(request);

                        var responseData = response.Content.ReadAsStringAsync().Result;
                        if (response.IsSuccessStatusCode)
                        {
                            // Edit User successfully
                            return(RedirectToAction("Index"));
                        }

                        // Edit User failed
                        ResponseObj resObj = JsonConvert.DeserializeObject <ResponseObj>(responseData);

                        ModelState.Clear();
                        ModelState.AddModelError("", "Error in editing User! Reason: " + resObj.Message);
                    }
                }
                else
                {
                    ModelState.Clear();
                    ModelState.AddModelError("", $"Error in editing User on form!");
                }

                return(View(userToUpdate));
            }
            catch (Exception ex)
            {
                object cauThongBao = $"Error in editing User!<br /> Reason: {ex.Message}";
                return(View("Error", cauThongBao));
            }
        }