示例#1
0
        public async Task <ActionResult> Create([Bind(Include = "id,UPhone,UPass,UAdress,Img,Roll_id,Pack_id,Exp_Date,AccNum")] Users users, HttpPostedFileBase Url)
        {
            string[] formats = new string[] { ".jpg", ".png", ".gif", ".jpeg" };
            ViewBag.Pack_id = new SelectList(db.Packs, "id", "name", users.Pack_id);
            string url_img = "";

            if (ModelState.IsValid)
            {
                if (Url != null)
                {
                    string path = "";
                    if (db.Users.Where(p => p.UPhone == users.UPhone).FirstOrDefault() != null)
                    {
                        ViewBag.ExErr = "This phone number has been registered before";
                        return(View(users));
                    }
                    try
                    {
                        path     = Path.Combine(Server.MapPath("~/images"), Path.GetFileName(Url.FileName));
                        url_img += Path.GetFileName(Url.FileName) + ",";
                    }
                    catch (Exception e)
                    {
                        ViewBag.FileStatus = "Error while file uploading.";
                    }
                    string ex = Path.GetExtension(Url.FileName);
                    if (!r.check(ex.ToLower(), formats))
                    {
                        ViewBag.FileStatus = ex + " is not an image";
                        return(View(users));
                    }
                    users.Img = url_img.Substring(0, url_img.Length - 1);
                    if (users.Pack_id == 1)
                    {
                        users.Exp_Date = DateTime.Now.AddMonths(1);
                    }
                    else if (users.Pack_id == 2)
                    {
                        users.Exp_Date = DateTime.Now.AddYears(1);
                    }
                    else
                    {
                        users.Exp_Date = DateTime.Now;
                    }
                    Url.SaveAs(path);
                    string hashed = r.HashPwd(users.UPass);
                    users.UPass = hashed;
                    //users.Roll_id = 2;
                    db.Users.Add(users);
                    await db.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }
            }
            ViewBag.Roll_id = new SelectList(db.Roles, "id", "name", users.Roll_id);
            return(View(users));
        }
示例#2
0
        public ActionResult ChangePwd(string oldp, string newp)
        {
            var    user    = (Users)Session["user"];
            string hashed  = r.HashPwd(oldp);
            var    isvalid = db.Users.Where(p => p.UPhone == user.UPhone && p.UPass == hashed).FirstOrDefault();

            ViewBag.old  = oldp;
            ViewBag.newp = newp;
            if (user != null && isvalid != null)
            {
                if (newp.Length < 8 || newp.Length > 50)
                {
                    ViewBag.err = "Password must be a 8-50 characters string ";
                    return(View());
                }
                isvalid.UPass = r.HashPwd(newp);
                db.SaveChanges();
                return(RedirectToAction("index", "Home"));
            }
            ViewBag.err = "Wrong credential";
            return(View());
        }