public void UpdateCookie(UserDTO dTO) { HttpCookie cookie = new HttpCookie("OnlineStoreCookie"); cookie.Value = EncryptionLib.EncryptCookie(dTO.UserToString()); HttpContext.Current.Response.SetCookie(cookie); HttpContext.Current.Response.Flush(); }
public Response <LoginResponse> CheckLogin(LoginModel loginModel) { try { using (var unitOfWork = new UnitOfWork()) { var user = unitOfWork.GetRepository <User>().Get(p => p.UserName.Equals(loginModel.UserName, StringComparison.Ordinal)); if (user != null) { if (EncryptionLib.EncryptText(loginModel.Password).Equals(user.Password)) { if (user.IsDelete) { return(new Response <LoginResponse>(0, "Tài khoản không tồn tại", null)); } if (user.IsDisable) { return(new Response <LoginResponse>(0, "Tài khoản của bạn đã bị khóa.", null)); } var department = unitOfWork.GetRepository <Department>().Get(s => s.DepartmentId == user.DepartmentId); user.LastLoginOnDate = DateTime.Now; user.SessionLoginCode = Guid.NewGuid(); unitOfWork.GetRepository <User>().Update(user); if (unitOfWork.Save() >= 1) { var response_ = new LoginResponse { Token = JWTEncryptionLib.GenerateToken(user.UserName, user.SessionLoginCode.ToString(), Convert.ToInt32(ConfigurationManager.AppSettings["TokenExpiry"])), User = new SessionModel { UserId = user.UserId, UserName = user.UserName, Department = department.Name, DepartmentId = user.DepartmentId, FullName = user.FullName, UserRole = user.UserRoleId } }; return(new Response <LoginResponse>(1, "", response_)); } return(new Response <LoginResponse>(0, "Đã xảy ra lỗi khi đăng nhập. Vui lòng thử lại", null)); } return(new Response <LoginResponse>(0, "Mật không không chính xác", null)); } return(new Response <LoginResponse>(0, "Tài khoản không tồn tại", null)); } } catch (Exception ex) { return(new Response <LoginResponse>(-1, ex.ToString(), null)); } }
public override void OnAuthorization(AuthorizationContext filterContext) { // Check the cookie and the session if (filterContext.HttpContext.Session["UserData"] == null) { if (filterContext.HttpContext.Request.Cookies.AllKeys.Contains("OnlineStoreCookie")) { string[] tokens = EncryptionLib.DecryptCookie(filterContext.HttpContext.Request.Cookies.Get("OnlineStoreCookie").Value.ToString()).Split('&'); if (tokens.Count() > 0) { UserDTO dto = new UserDTO() { UserId = Convert.ToInt64(tokens[0]), UserName = tokens[1], UserRole = new OnlineStore.Application.Roles.RoleDTO() { RoleId = Convert.ToInt32(tokens[2]) }, Email = tokens[3] }; filterContext.HttpContext.Session["UserData"] = dto; } else { filterContext.Result = new RedirectToRouteResult( new RouteValueDictionary { { "controller", "Account" }, { "action", "Login" }, { "area", "" } }); } } else { filterContext.Result = new RedirectToRouteResult( new RouteValueDictionary { { "controller", "Account" }, { "action", "Login" }, { "area", "" } }); } } //base.OnAuthorization(filterContext); }
public Response <UserModel> Create(UserCreateModel createModel) { try { using (var unitOfWork = new UnitOfWork()) { var last = unitOfWork.GetRepository <User>().GetAll().OrderByDescending(c => c.UserId).FirstOrDefault(); if ((createModel.Password.Length < 6) || (createModel.Password.Length > 18)) { return(new Response <UserModel>(0, "Mật khẩu phải có từ 6-18 ký tự", null)); } if ((createModel.UserName.Length < 6) || (createModel.UserName.Length > 18)) { return(new Response <UserModel>(0, "Tên tài khoản phải có từ 6-18 ký tự", null)); } User user = new User { DepartmentId = createModel.DepartmentId, FullName = createModel.FullName, IsDelete = false, UserId = 1, CreateOnDate = DateTime.Now, Password = EncryptionLib.EncryptText(createModel.Password), UserName = createModel.UserName, UserRoleId = 2 }; if (last != null) { user.UserId = last.UserId + 1; } unitOfWork.GetRepository <User>().Add(user); if (unitOfWork.Save() >= 1) { return(GetById(user.UserId)); } return(new Response <UserModel>(0, "Lưu thông tin không thành công", null)); } } catch (Exception ex) { return(new Response <UserModel>(-1, ex.ToString(), null)); } }
public Response <UserModel> Update(int Id, UserUpdateModel userUpdateModel) { try { using (var unitOfWork = new UnitOfWork()) { if ((userUpdateModel.Password.Length < 6) || (userUpdateModel.Password.Length > 18)) { return(new Response <UserModel>(0, "Mật khẩu phải có từ 6-18 ký tự", null)); } if ((userUpdateModel.UserName.Length < 6) || (userUpdateModel.UserName.Length > 18)) { return(new Response <UserModel>(0, "Tên tài khoản phải có từ 6-18 ký tự", null)); } var user = unitOfWork.GetRepository <User>().GetById(Id); if (user != null) { user.FullName = userUpdateModel.FullName; user.DepartmentId = userUpdateModel.DepartmentId; user.Password = EncryptionLib.EncryptText(userUpdateModel.Password); user.DepartmentId = userUpdateModel.DepartmentId; unitOfWork.GetRepository <User>().Update(user); if (unitOfWork.Save() >= 1) { return(GetById(user.UserId)); } return(new Response <UserModel>(0, "Lưu thông tin không thành công", null)); } else { return(new Response <UserModel>(0, "Không tìm thấy người dùng", null)); } } } catch (Exception ex) { return(new Response <UserModel>(-1, ex.ToString(), null)); } }