public static AdminUserModel GetLoginUser(string token) { if (string.IsNullOrEmpty(token)) { return(null); } var adminUser = CacheHelper.GetCache(token) as AdminUserModel; if (adminUser == null) { return(null); } if (adminUser.TokenExpired < DateTime.Now.AddMinutes(30)) { var adminUserBLL = new TAdminUserBLL(); var refreshAdminUser = adminUserBLL.Find(u => (u.Token == adminUser.Token && u.TokenExpired > DateTime.Now)); if (refreshAdminUser != null) { refreshAdminUser.TokenExpired = DateTime.Now.AddHours(2); adminUserBLL.Edit(refreshAdminUser, new string[] { "TokenExpired" }, false); adminUserBLL.SaveChanges(); CacheHelper.SetCache(refreshAdminUser.Token, refreshAdminUser, new TimeSpan(0, 30, 0)); } else { CacheHelper.RemoveAllCache(adminUser.Token); return(null); } } return(adminUser); }
public APIJsonResult Edit(AdminUserEditModel adminUserModel) { var adminUserBLL = new TAdminUserBLL(); var adminUser = adminUserBLL.Find(u => u.ID == adminUserModel.ID); AddUpdateInfo(adminUser); List <string> updatedField = new List <string>(); if (!string.IsNullOrEmpty(adminUserModel.Password)) { adminUser.Password = EncryptHelper.EncryptString(adminUserModel.Password); } adminUser.Status = adminUserModel.Status; adminUserBLL.SaveChanges(); var adminUserRoleBLL = new TAdminUserRoleBLL(); var adminUserRole = adminUserRoleBLL.Find(r => r.AdminUserID == adminUser.ID); adminUserRole.RoleID = adminUserModel.RoleID; AddUpdateInfo(adminUserRole); adminUserRoleBLL.SaveChanges(); return(Success()); }
public static void RemoveLoginUser() { string token = GetRequestToken(); var adminUserBLL = new TAdminUserBLL(); var refreshAdminUser = adminUserBLL.Find(u => u.Token == token); if (refreshAdminUser != null) { refreshAdminUser.Token = string.Empty; refreshAdminUser.TokenExpired = null; adminUserBLL.SaveChanges(); } CacheHelper.RemoveAllCache(token); }
public APIJsonResult Login(LoginModel adminUserModel) { var adminUser = adminUserBLL.Find(user => user.Account == adminUserModel.Account && user.Status == UserStatusList.Active); if (adminUser == null || !UtilHelper.CompareString(adminUser.Password, EncryptHelper.EncryptString(adminUserModel.Password))) { return(Failed("Account and password do not match.")); } adminUser.LastLoginDate = DateTime.Now; adminUser.LastLoginIP = Request.ServerVariables.Get("Remote_Addr").ToString(); adminUser.Token = LoginManager.GenerateToken(); adminUser.TokenExpired = DateTime.Now.AddHours(2); adminUserBLL.SaveChanges(); LoginManager.SaveLoginUser(adminUser); return(Success("", new { Token = adminUser.Token, AdminUser = LoginManager.GetLoginUser(adminUser.Token) })); }