public IActionResult Index() { if (User.Identity.IsAuthenticated) { var email = User.Claims.SingleOrDefault(o => o.Type == ClaimTypes.Email).Value; if (!string.IsNullOrEmpty(email)) { currentUser = _userService.GetItemByEmail(email); ViewBag.Data = currentUser; } } return(View()); }
public async Task <IActionResult> Create(DefaultModel model, CPUserEntity item) { if (_service.GetItemByEmail(item.Email) != null) { ViewBag.Message = "Email đã tồn tại"; return(View()); } ViewBag.Title = "Thêm mới"; if (!string.IsNullOrEmpty(model.ID) || !string.IsNullOrEmpty(item.ID)) { return(RedirectToAction("Edit", new { model.ID })); } else { item.Pass = Security.Encrypt(item.Pass); await _service.AddAsync(item); } ViewBag.RoleData = _listRoles; return(View()); }
private static ClaimsPrincipal GetCurrentUser(this HttpContext context) { string token = context.GetValue(Cookies.DefaultLogin, false); if (string.IsNullOrEmpty(token)) { return(null); } else { // neeus co cache var cache = CacheExtends.GetDataFromCache <ClaimsPrincipal>(token); if (cache != null) { return(cache); } // ko co cache var logs = new CPLoginLogService(); string email = logs.GetEmailFromDb(token); if (string.IsNullOrEmpty(email)) { return(null); } else { var account = new CPUserService(); var user = account.GetItemByEmail(email); if (user == null) { return(null); } else { var role = new CPRoleService(); var irole = role.GetItemByID(user.RoleID); if (role == null) { return(null); } var claims = new List <Claim> { new Claim(ClaimTypes.Email, user.Email), new Claim(ClaimTypes.Name, user.Name), new Claim(ClaimTypes.Role, irole.Code), new Claim("RoleID", irole.ID.ToString()) }; var claimsIdentity = new ClaimsIdentity(claims, Cookies.DefaultLogin); var authenProperties = new AuthenticationProperties { IsPersistent = true, ExpiresUtc = DateTime.UtcNow.AddMinutes(Cookies.ExpiresLogin) }; ClaimsPrincipal claim = new ClaimsPrincipal(); claim.AddIdentity(claimsIdentity); CacheExtends.SetObjectFromCache(token, Cookies.ExpiresLogin, claim); return(claim); } } } }