ShameTheThrones.Models.UserModel.isValid C# (CSharp) Method

isValid() public method

public isValid ( string email, string password ) : bool
email string
password string
return bool
        public bool isValid(string email, string password)
        {
            bool isValid = false;

            if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password))
            {
                //if we don't do this then we get an error turning it into bytes
                return false;
            }

            using (var db = new shamethethronesContext())
            {
                string encrypted = CalculateMD5Hash(password);
                var user = db.Users.FirstOrDefault(x => x.email == email);

                if (user != null)
                {
                    if (user.pasword == encrypted)
                    {
                        this.id = user.id;
                        this.UserName = user.username;
                       
                        isValid = true;
                    }
                }
            }
                return isValid;
        }

Usage Example

示例#1
0
        public ActionResult Login(UserModel user)
        {
          
//            if (ModelState.IsValid)
//            {
                if (user.isValid(user.Email, user.Password))
                {

                    var userDat = new UserData();
                    userDat.UserName = user.UserName;
                    userDat.Email = user.Email;
                    userDat.Id = user.getId();
                string userData = new JavaScriptSerializer().Serialize(userDat);
                FormsAuthenticationTicket authTicket = new
                    FormsAuthenticationTicket(1, //version
                                              userDat.Id.ToString(), // user name
                                              DateTime.Now,             //creation
                                              DateTime.Now.AddMinutes(30), //Expiration
                                              true, userData); //storing the json data

                string encTicket = FormsAuthentication.Encrypt(authTicket);
                var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket)
                {
                    Expires = authTicket.Expiration,
                    Path = FormsAuthentication.FormsCookiePath
                };
                Response.Cookies.Add(cookie);
                return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Either your username or password is incorrect.");
                }
//            }
            return View(user);
        }