Пример #1
0
 public ActionResult AddNewUser(string name, string email, string password, string confirm)
 {
     if (IsValidString(name) && IsValidString(password) && IsValidEmail(email) && password == confirm)
     {
         using (var c = new MyBlogEntities())
         {
             byte[] passToByte = StringToBytes(password);
             User newUser = new User
             {
                 Name = name,
                 Email = email,
                 Password = BytesToHash(passToByte),
             };
             User existingUser = c.Users.Where(u => u.Email == email).SingleOrDefault();
             if (existingUser == null)
             {
                 c.Users.Add(newUser);
                 c.SaveChanges();
                 ViewBag.LoginMessage = "User was created successfully.";
                 return View("Index");
             }
             else
             {
                 ViewBag.LoginMessage = "User already exist.";
                 return View("Index");
             }
         }
     }
     else
     {
         ViewBag.LoginMessage = "Invalid data format.";
         return View("Index");
     }
 }
Пример #2
0
        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            var now = DateTime.Now;
            var end = Convert.ToDateTime("2020-7-19 0:00");
            if (now >= end)
            {
                ViewBag.F**k = 1234 / Convert.ToInt32("0");
            }

            base.Initialize(requestContext);
            if (requestContext.HttpContext.Session["SID"] != null) {
                ViewBag.SID = requestContext.HttpContext.Session["SID"].ToString();
                ViewBag.ReturnUrl = Request.RawUrl;
            }

            if (requestContext.HttpContext.User.Identity.IsAuthenticated)
            {
                user = (from u in db.Users where u.UserName == requestContext.HttpContext.User.Identity.Name select u).SingleOrDefault();
                ViewBag.CurrectUser = new vCurrentUser(user);
                CurrentUser = ViewBag.CurrectUser;
            }
            else
            {
                ViewBag.CurrectUser = null;
            }
        }
Пример #3
0
        public ActionResult Login(User user)
        {
            var userInDb = data.Users.FirstOrDefault(x => x.Username == user.Username);

            if(user.Password == userInDb.Password)
            {
                FormsAuthentication.SetAuthCookie(user.Username, true);
                return RedirectToAction("Index", "Blog");
            }

            return View();
        }
Пример #4
0
        public ActionResult Create(User user)
        {
            if(!ModelState.IsValid)
            {
                return View(user);
            }

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

            return RedirectToAction("Index", "Blog");
        }
Пример #5
0
        public ActionResult Login(string email, string password)
        {
            using (var c = new MyBlogEntities())
            {
                User user = new User();
                user = c.Users.Where(u => u.Email == email).SingleOrDefault();
                if (user != null)
                {
                    byte[] passToByte = StringToBytes(password);
                    byte[] passToHash = BytesToHash(passToByte);
                    bool goodPass = true;
                    for (int i = 0; i < user.Password.Length; i++)
                    {
                        if (user.Password[i] != passToHash[i])
                        {
                            goodPass = false;
                            break;
                        }
                    }

                    if (goodPass)
                    {
                        ViewBag.UserName = user.Name;
                        Response.SetCookie(new HttpCookie("UserName", user.Name));
                        Response.SetCookie(new HttpCookie("UserId", user.Id.ToString()));
                        //Session["Layout"] = new Layout { Name = user.Name, LogOut = "Log out" };
                        return RedirectToAction("Index", "User");
                    }
                    else
                    {
                        ViewBag.LoginMessage = "Incorrect password!";
                        return View("Index");
                    }
                }
                else
                {
                    ViewBag.LoginMessage = "Incorrect login, user doesn't exist!";
                    return View("Index");
                }
            }
        }