Пример #1
0
        public ActionResult Register([Bind(Exclude = "activeFlag")] user user)
        {
            bool   Status  = false;
            String Message = "";

            //model validation
            if (ModelState.IsValid)
            {
                #region //email is already Exits
                var isExist = IsEmailExist(user.email);
                if (isExist)
                {
                    ModelState.AddModelError("EmailExist", "Email already exist");
                    return(View(user));
                }
                #endregion

                #region Generate activeFlag
                user.activeFlag = 0;
                user.createDate = DateTime.Now;
                user.updateDate = DateTime.Now;


                #endregion

                #region password hashing
                user.password = UtilPass.Hash(user.password);
                #endregion


                #region Save data database
                using (dotnetstorephoneEntities phone_Store = new dotnetstorephoneEntities())
                {
                    phone_Store.users.Add(user);
                    phone_Store.SaveChanges();

                    //Send Email user
                    SendVerificationSendLinkEmail(user.email, user.id);
                    Message = "We have sent you a confirmation link at the email:" + user.email + ".Please confirm!";
                    Status  = true;
                    /*return RedirectToAction("Login", "MyUser");*/
                }
                #endregion
            }
            else
            {
                Message = "Invalid Request";
            }

            ViewBag.Message = Message;
            ViewBag.Status  = Status;

            return(View(user));
        }
Пример #2
0
        public ActionResult Login(UserLogin userLogin, string ReturnUrl = "")
        {
            #region encode

            /* string message = "";
             * if (ModelState.IsValid)
             * {
             *   using (phone_storeEntities ps = new phone_storeEntities())
             *   {
             *       string password = UtilPass.Hash(userLogin.Password);
             *       var v = ps.users.Where(a => a.username.Equals(userLogin.Username) && a.password.Equals(password)&& a.activeFlag==1).FirstOrDefault();
             *
             *       if (v != null)
             *       {
             *           FormsAuthentication.SetAuthCookie(v.username, userLogin.RememberMe);
             *           if (Url.IsLocalUrl(ReturnUrl))
             *           {
             *
             *               return Redirect(ReturnUrl);
             *           }
             *           else
             *           {
             *               int timeout = userLogin.RememberMe ? 525600 : 20;
             *               var ticket = new FormsAuthenticationTicket(userLogin.Username, userLogin.RememberMe, timeout);
             *               string encryted = FormsAuthentication.Encrypt(ticket);
             *               var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryted);
             *               cookie.Expires = DateTime.Now.AddMinutes(timeout);
             *               cookie.HttpOnly = true;
             *               Response.Cookies.Add(cookie);
             *               return RedirectToAction("Index", "Home");
             *           }
             *       }
             *       else
             *       {
             *           message = "Account does not exist!";
             *       }
             *   }
             * }
             * else
             * {
             *   message = "Please enter username and password!";
             * }
             * ViewBag.Message = message;*/
            #endregion
            string message = "";
            //aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa phân quyền
            if (ModelState.IsValid)
            {
                string password   = UtilPass.Hash(userLogin.Password);
                var    isValiUser = Membership.ValidateUser(userLogin.Username, password);


                dotnetstorephoneEntities1 p = new dotnetstorephoneEntities1();
                var roles = (from a in p.roles
                             join b in p.roleusers on a.id equals b.roleId
                             join c in p.users on b.userId equals c.id
                             where c.username.Equals(userLogin.Username)
                             select a.roleName).ToArray <string>();



                foreach (string i in roles)
                {
                    if (isValiUser)
                    {
                        FormsAuthentication.SetAuthCookie(userLogin.Username, userLogin.RememberMe);

                        if (Url.IsLocalUrl(ReturnUrl))
                        {
                            return(Redirect(ReturnUrl));
                        }
                        else
                        {
                            if (i.Contains("Admin"))
                            {
                                return(RedirectToAction("Index", "Admin/HomeAdmin"));
                            }

                            /*if (i.Equals("AdminProduct"))
                             * {
                             *  return RedirectToAction("add_Product", "Admin/HomeAdmin");
                             * }*/
                        }
                    }
                    else
                    {
                        message = " Invalid Request! Your account is not activated yet.Please check your Email";
                    }
                }
                if (isValiUser)
                {
                    FormsAuthentication.SetAuthCookie(userLogin.Username, userLogin.RememberMe);
                    if (Url.IsLocalUrl(ReturnUrl))
                    {
                        return(Redirect(ReturnUrl));
                    }
                    else
                    {
                        return(RedirectToAction("Index", "Home"));
                    }
                }
                else
                {
                    message = " Invalid Request! Your account is not activated yet.Please check your Email";
                }
            }

            ViewBag.Message = message;
            ModelState.Remove("password");
            return(View());
        }