public ActionResult Index()
 {
     User userID = new User();
     userID = db.Users.FirstOrDefault(u => u.Username == User.Identity.Name);
     var myBookmarks = db.Bookmarks.Where(b => b.UserID == userID.UserID);
     return View(myBookmarks.ToList());
 }
Beispiel #2
0
 public ActionResult Index()
 {
     if (User.Identity.IsAuthenticated)
     {
         User userID = new User();
         userID = db.Users.FirstOrDefault(u => u.Username == User.Identity.Name);
         UserAuthToken uat = new UserAuthToken();
         uat = db.UserAuthTokens.First(u => u.UserID == userID.UserID);
         ViewBag.uat = uat.AuthToken;
     }
         return View();
 }
Beispiel #3
0
        public ActionResult Create([Bind(Include = "Username,Password,ConfirmPassword, Email")] UserCreateViewModel userVM)
        {
            if (db.Users.Any(u => u.Email == userVM.Email))
            {
                ModelState.AddModelError("Email", "Email in use");
            }

            if (db.Users.Any(u => u.Username == userVM.Username))
            {
                ModelState.AddModelError("Username", "Username in use");
            }

            //UserID,Salt,IsEmailVerified,IsActive;
            if (ModelState.IsValid)
            {

                User user = new User();
                user.Username = userVM.Username;
                //user.Password = userVM.Password;
                user.Email = userVM.Email;
                //user.Salt = BCryptHelper.GenerateSalt();
                string pwdToHash = userVM.Password + "*)&h9";
                user.Password = BCryptHelper.HashPassword(pwdToHash, BCryptHelper.GenerateSalt());

                //db.Users.Add(user);
                //db.SaveChanges();
                //try
                //{
                //    db.SaveChanges();
                //}
                //catch (DbUpdateException e)
                //{
                //    //if(e.InnerException.ToString().Contains("Cannot insert duplicate key row in object 'redxadmin.Users' with unique index 'IX_Email'"))
                //    if(db.Users.Any(u => u.Email == userVM.Email))
                //    {
                //        ModelState.AddModelError("Email", "Email in use");
                //    }
                //    //if (e.InnerException.ToString().Contains("Cannot insert duplicate key row in object 'redxadmin.Users' with unique index 'IX_Username'"))
                //    if(db.Users.Any(u => u.Username == userVM.Username))
                //    {
                //        ModelState.AddModelError("Username", "Username in use");
                //    }
                //    return View(userVM);
                //}
                FormsAuthentication.SetAuthCookie(userVM.Username, false);

                //MailMessage mMsg = new MailMessage("*****@*****.**", user.Email);
                //mMsg.Subject = "Account created";
                //mMsg.Body = "An account with username " + user.Username + " has been created on bookmarky.redx.rocks";
                //SmtpClient smtpClient = new SmtpClient();
                //smtpClient.Send(mMsg);
                //smtpClient.Dispose();

                db.Users.Add(user);
                db.SaveChanges();

                UserAuthToken uat = new UserAuthToken();
                uat.UserID = user.UserID;
                uat.AuthToken = BCryptHelper.GenerateSalt();
                uat.ExpireDate = DateTime.Now;

                db.UserAuthTokens.Add(uat);
                db.SaveChanges();

                return RedirectToAction("Index", "Home");
            }
            return View(userVM);
        }