Example #1
0
        public ActionResult Login(MemberLoginVM model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            bool isValid = false;

            using (Db db = new Db()) {
                MemberFeatures passwordHash = new MemberFeatures();
                string         hashPassword = passwordHash.HashPassword(model.Password);

                if (db.Members.Any(x => x.Account.Equals(model.UserName) && x.Password.Equals(hashPassword)))
                {
                    isValid = true;
                }

                if (!isValid)
                {
                    ModelState.AddModelError("", "帳號或密碼錯誤");
                    return(View(model));
                }
                else
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                    return(Redirect(FormsAuthentication.GetRedirectUrl(model.UserName, model.RememberMe)));
                }
            }
        }
Example #2
0
        public ActionResult Login(MemberLoginVM LoginMember)
        {
            if (ModelState.IsValid)
            {
                string ValidateStr = MemberService.LoginCheck(LoginMember.UserName, LoginMember.Password);
                if (string.IsNullOrEmpty(ValidateStr))
                {
                    string RoleData = MemberService.GetRole(LoginMember.UserName);

                    FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(
                        1,
                        LoginMember.UserName,
                        DateTime.Now,
                        DateTime.Now.AddMinutes(30),
                        false,
                        RoleData,
                        FormsAuthentication.FormsCookiePath);
                    string enTicket = FormsAuthentication.Encrypt(Ticket);
                    Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, enTicket));
                    return(RedirectToAction("Index", "Guestbook"));
                }
                else
                {
                    ModelState.AddModelError("", ValidateStr);
                    LoginMember.Password = null;
                    return(View(LoginMember));
                }
            }

            LoginMember.Password = null;
            return(View(LoginMember));
        }
Example #3
0
        public ActionResult Login(MemberLoginVM member, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                string ValidateStr = _memberService.LoginCheck(member.Username, member.Password);

                if (string.IsNullOrEmpty(ValidateStr))
                {
                    string RoleData = _memberService.GetRole(member.Username);

                    FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(
                        version: 1,
                        name: member.Username,
                        issueDate: DateTime.Now,
                        expiration: DateTime.Now.AddMinutes(30),
                        isPersistent: false,
                        userData: RoleData,
                        cookiePath: FormsAuthentication.FormsCookiePath);

                    string enTicket = FormsAuthentication.Encrypt(Ticket);
                    Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, enTicket));

                    if (string.IsNullOrEmpty(returnUrl))
                    {
                        return(RedirectToAction("Index", "Home"));
                    }
                    else
                    {
                        return(Redirect(returnUrl));
                    }
                }
                else
                {
                    ModelState.AddModelError("", ValidateStr);
                    member.Password = null;
                    return(View(member));
                }
            }
            else
            {
                //debug
                ModelState.AddModelError("", "模型驗證不正確");
                member.Password = null;
                return(View(member));
            }
        }