Exemple #1
0
        public async Task <IActionResult> SignIn(UserCredentialViewModel userCredentialViewModel)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Index"));
            }

            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Login", "Home"));
            }

            var response = await AdminHttpClient.PostAsync(WebConfiguration.Instance.WebApiConfig, "api/Auth/Login", userCredentialViewModel, Request);

            if (!response.IsSuccessStatusCode)
            {
                return(RedirectToAction("Login", "Home", "", "LoginFail"));
            }

            var credential = await response.Content.ReadAsAsync <UserCredentialViewModel>();

            CookieOptions cookieOptions = new CookieOptions
            {
                Path        = "/",
                Expires     = DateTime.Now.AddHours(5),
                SameSite    = SameSiteMode.Strict,
                IsEssential = true
            };
            var val = credential.Token;

            Response.Cookies.Append(ConfigKeys.AuthCookieKey, val, cookieOptions);

            var model = await AdminHttpClient.GetAsync <RegistrationModelView>(WebConfiguration.Instance.WebApiConfig, $"api/Registration/{userCredentialViewModel.UserName}/token", credential.Token);

            if (model != null)
            {
                val = HttpUtility.UrlEncode(JsonConvert.SerializeObject(
                                                new
                {
                    FullName = model.FirstLastName,
                    model.Email,
                    model.Id,
                    model.MobileNumber
                }, Formatting.None));

                Response.Cookies.Append(ConfigKeys.UserCookieKey, val, cookieOptions);
            }

            return(RedirectToAction("Index", "Home"));
        }
Exemple #2
0
        public async Task <IActionResult> Logout()
        {
            try
            {
                await AdminHttpClient.PostAsync(WebConfiguration.Instance.WebApiConfig, "/api/Auth/Logout", (object)null, Request);

                Response.Cookies.Delete("Auth");
                Response.Cookies.Delete("user");

                return(RedirectToAction("Login"));
            }
            catch (Exception e)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, e));
            }
        }
        public async Task <IActionResult> Create(RegistrationModelView model)
        {
            if (ModelState.IsValid)
            {
                model.CreateDate = DateTime.UtcNow;
                model.CreateBy   = 1;//currentUser.Id;
                var response = await AdminHttpClient.PostAsync(WebConfiguration.Instance.WebApiConfig, $"api/Registration", model, Request);

                if (response.IsSuccessStatusCode)
                {
                    return(RedirectToAction("Index"));
                }
                else
                {
                    return(View(model));
                }
            }
            return(View(model));
        }