public ActionResult Create(TblUser TblUser, FormCollection collection) { string username = TblUser.UserName; var kiemtra = db.TblUser.Where(p => p.UserName == username).ToList(); if (kiemtra.Count > 0) { ViewBag.thongbao = "<div class=\"alert alert-info\">Tài khoản bạn đăng đã tồn tại, vui lòng nhập tên tài khoản khác !<button class=\"close\" data-dismiss=\"alert\">×</button></div>"; } else { TblUser.Password = EncryptandDecrypt.Encrypt(TblUser.Password); TblUser.DateCreate = DateTime.Now; TblUser.Gender = int.Parse(collection["ddlGender"]); string IdUser = Request.Cookies["Username"].Values["UserID"]; TblUser.IdUser = int.Parse(IdUser); db.TblUser.Add(TblUser); db.SaveChanges(); #region [Updatehistory] #endregion if (collection["btnSave"] != null) { Session["Thongbao"] = "<div class=\"alert alert-info alert1\">Bạn đã thêm tài khoản thành công !<button class=\"close\" data-dismiss=\"alert\">×</button></div>"; return(Redirect("/Users/Index")); } if (collection["btnSaveCreate"] != null) { Session["Thongbao"] = "<div class=\"alert alert-info\">Bạn đã thêm tài khoản thành công, mời bạn thêm tài khoản mới !<button class=\"close\" data-dismiss=\"alert\">×</button></div>"; return(Redirect("/Users/Create")); } return(Redirect("Index")); } return(View()); }
public ActionResult LoginIndex(tblUser tblusers, tblHistoryLogin tblhistorylogin) { string pass = EncryptandDecrypt.Encrypt(tblusers.Password); var list = db.tblUsers.Where(p => p.UserName == tblusers.UserName && p.Password == pass && p.Active == true).ToList(); if (list.Count > 0) { var userCookie = new HttpCookie("Username"); var id = list[0].id.ToString(CultureInfo.InvariantCulture); var username = list[0].UserName; var fullname = list[0].FullName; userCookie.Values["UserID"] = id; userCookie.Values["FullName"] = Server.HtmlEncode(fullname.Trim()); userCookie.Values["fullname"] = fullname; userCookie.Values["UserName"] = Server.UrlEncode(username.Trim()); userCookie.Values["username"] = username; tblhistorylogin.FullName = fullname; tblhistorylogin.Task = "Login hệ thống"; tblhistorylogin.idUser = int.Parse(id); tblhistorylogin.DateCreate = DateTime.Now; tblhistorylogin.Active = true; db.tblHistoryLogins.Add(tblhistorylogin); db.SaveChanges(); userCookie.Expires = DateTime.Now.AddHours(22); Response.Cookies.Add(userCookie); Session["Count"] = ""; return(Redirect("/Productad/Index")); } else { ViewBag.Note = "Tài khoản của bạn nhập không đúng, kiểm tra lại tên đăng nhập hoặc mật khẩu. Có thể tài khoản bị khóa !"; return(View()); } }
public HttpResponseMessage Login(User user) { try { if (register.Login(user)) { EncryptandDecrypt objencrypt = new EncryptandDecrypt(); Thread.CurrentPrincipal = new GenericPrincipal( new GenericIdentity(user.UserName), null); user.user_token = objencrypt.encrypt(user.UserName); FormsAuthentication.SetAuthCookie(user.UserName, false, ""); var response = Request.CreateResponse <User>(HttpStatusCode.OK, user); return(response); } return(Request.CreateResponse(HttpStatusCode.Unauthorized)); } catch (Exception ex) { throw; } }
public ActionResult Login([Bind(Include = "UserId,UserName,PasswordHash,DisplayName,Email,DateCreated,DateModfied,DateDeleted")] User user) { String passwordhash = EncryptandDecrypt.Crypt(user.PasswordHash); User cuser = userService.Load(x => x.UserName == user.UserName && x.PasswordHash == passwordhash && !x.DateDeleted.HasValue).SingleOrDefault(); if (cuser != null) { Session["DisplayName"] = cuser.DisplayName; Session["UserName"] = cuser.UserName; Session["UserId"] = cuser.UserId; ViewBag.Error = "User Logged IN"; return(RedirectToAction("Index", "Forms")); } TempData["Error"] = "UserName or Password do not match"; ViewBag.Error = "UserName or Password do not match"; return(RedirectToAction("Login", "Users")); }
public ActionResult Register([Bind(Include = "UserId,UserName,PasswordHash,DisplayName,Email,DateCreated,DateModfied,DateDeleted")] User user) { if (ModelState.IsValid) { User existinguser = userService.Load(u => u.UserName == user.UserName).SingleOrDefault(); if (existinguser == null) { user.PasswordHash = EncryptandDecrypt.Crypt(user.PasswordHash); user.DateCreated = DateTime.Now; userService.Add(user); userService.Save(); Session["UserName"] = user.UserName; Session["DisplayName"] = user.DisplayName; Session["UserId"] = user.UserId; return(RedirectToAction("Index", "Forms")); } TempData["Error"] = "User with given Username already existed."; return(RedirectToAction("Register", "Users")); } return(View(user)); }
public ActionResult Setting(String oldPassword, String newPassword) { int UserId = Convert.ToInt32(Session["UserId"]); User dbUser = userService.LoadByID(UserId); String dbPasswordHash = dbUser.PasswordHash; String dbPasswordUnhash = EncryptandDecrypt.Decrypt(dbPasswordHash); bool PasswordMatch = String.Equals(dbPasswordUnhash, oldPassword); if (!PasswordMatch) { TempData["Error"] = "Old Password did not match our Records"; return(RedirectToAction("Setting", "Users")); } else { dbUser.PasswordHash = EncryptandDecrypt.Crypt(newPassword); dbUser.DateModfied = DateTime.Now; userService.Save(); TempData["Success"] = "Password Updated Successfully"; return(RedirectToAction("Setting", "Users")); } }
public ActionResult Edit(tblUser tbluser, int id, FormCollection collection) { if (ModelState.IsValid) { db.Entry(tbluser).State = EntityState.Modified; var user = db.tblUsers.First(p => p.id == id); string pass = tbluser.Password; if (pass == user.Password) { tbluser.Password = EncryptandDecrypt.Encrypt(tbluser.Password); } else { tbluser.Password = EncryptandDecrypt.Encrypt(tbluser.Password); } tbluser.UserName = user.UserName; tbluser.DateCreate = DateTime.Now; string idUser = Request.Cookies["Username"].Values["UserID"]; tbluser.idUser = int.Parse(idUser); db.SaveChanges(); #region [Updatehistory] Updatehistoty.UpdateHistory("Edit User", Request.Cookies["Username"].Values["FullName"].ToString(), Request.Cookies["Username"].Values["UserID"].ToString()); #endregion if (collection["btnSave"] != null) { Session["Thongbao"] = "<div class=\"alert alert-info alert1\">Bạn đã sửa tài khoản thành công !<button class=\"close\" data-dismiss=\"alert\">×</button></div>"; return(Redirect("/Users/Index")); } if (collection["btnSaveCreate"] != null) { Session["Thongbao"] = "<div class=\"alert alert-info\">Bạn đã thêm tài khoản thành công, mời bạn thêm tài khoản mới !<button class=\"close\" data-dismiss=\"alert\">×</button></div>"; return(Redirect("/Users/Create")); } } return(View(tbluser)); }