public async Task <IActionResult> Logout() { await HttpContext.SignOutAsync( CookieAuthenticationDefaults.AuthenticationScheme); var CartSessionKey = _configuration.GetSection("CartSessionKey").Value; CookieHelpers.RemoveCookie(Response.Cookies, CartSessionKey); CookieHelpers.RemoveCookie(Response.Cookies, "CartId"); CookieHelpers.RemoveCookie(Response.Cookies, "Token"); return(RedirectToAction("index", "Login")); }
public async Task <IActionResult> Index(LoginRequest request, [FromQuery] string ReturnUrl) { if (!ModelState.IsValid) { return(View(ModelState)); } var result = await _userAPIClient.Authenticate(request); if (result.IsSuccessed == false) { TempData["message"] = result.Message; ModelState.AddModelError("", result.Message); ViewBag.ErrorServerSide = true; return(View()); } TempData["Succes"] = "Login Succsess!"; CookieHelpers.SetObjectAsJson(Response.Cookies, "Token", result.ResultObject, 10); var userPrincipal = this.ValidateToken(result.ResultObject); var UserId = new Guid(userPrincipal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value); var cartResult = await _cartService.GetById(UserId); if (cartResult.IsSuccessed == true) { var CartSessionKey = _configuration.GetSection("CartSessionKey").Value; CookieHelpers.RemoveCookie(Response.Cookies, CartSessionKey); CookieHelpers.SetObjectAsJson(HttpContext.Response.Cookies, CartSessionKey, cartResult.ResultObject.CartItems, null); } var authProperties = new AuthenticationProperties { ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(10), IsPersistent = true // có sử dụng persistent cookie }; await HttpContext.SignInAsync( CookieAuthenticationDefaults.AuthenticationScheme, userPrincipal, authProperties); if (!string.IsNullOrEmpty(ReturnUrl)) { return(Redirect(ReturnUrl)); } else { return(RedirectToAction("index", "home")); } }
public async Task <IActionResult> ExternalLoginCallback(string returnUrl, string remoteError = null) { ExternalLoginInfo info = await _signInManager.GetExternalLoginInfoAsync(); returnUrl = returnUrl ?? Url.Content("~/"); if (remoteError != null) { ModelState.AddModelError(string.Empty, $"Error from external provider:{remoteError}"); return(View("Index")); } var externalLoginRequest = new ExternalLoginRequest { FullName = info.Principal.FindFirstValue(ClaimTypes.Name), Email = info.Principal.FindFirstValue(ClaimTypes.Email), ProviderKey = info.ProviderKey, LoginProvider = info.LoginProvider, ProviderDisPlayName = info.ProviderDisplayName, ImagePath = info.Principal.Claims.FirstOrDefault(c => c.Type == "picture").Value, }; var result = await _userAPIClient.ExternalLoginCallback(externalLoginRequest); if (result.IsSuccessed == false) { TempData["message"] = result.Message; ModelState.AddModelError("", result.Message); ViewBag.ErrorServerSide = true; return(View()); } TempData["Succes"] = "Login Succsess!"; //HttpContext.Session.SetString("Token", result.ResultObject); CookieHelpers.SetObjectAsJson(Response.Cookies, "Token", result.ResultObject, 10); var userPrincipal = this.ValidateToken(result.ResultObject); var UserId = new Guid(userPrincipal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value); var cartResult = await _cartService.GetById(UserId); if (cartResult.IsSuccessed == true) { var CartSessionKey = _configuration.GetSection("CartSessionKey").Value; CookieHelpers.RemoveCookie(Response.Cookies, CartSessionKey); CookieHelpers.SetObjectAsJson(HttpContext.Response.Cookies, CartSessionKey, cartResult.ResultObject.CartItems, null); } var authProperties = new AuthenticationProperties { ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(10), IsPersistent = false // có sử dụng persistent cookie }; await HttpContext.SignInAsync( CookieAuthenticationDefaults.AuthenticationScheme, userPrincipal, authProperties); if (!string.IsNullOrEmpty(returnUrl)) { return(Redirect(returnUrl)); } else { return(RedirectToAction("index", "home")); } }