Exemplo n.º 1
0
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            ControllerContext.HttpContext.Session["loggedInUser"] = null;
            if (ModelState.IsValid)
            {
                List<User> present = entities.Users.Where(x => x.Username == model.UserName && x.Password == model.Password).ToList();

                if (present.Count == 1)
                {
                    if (model.RememberMe)
                    {
                        if (System.Web.HttpContext.Current.Response.Cookies["coolCookie"] == null)
                        {
                            HttpCookie cookie = new HttpCookie("coolCookie");
                            string encrypted = Convert.ToBase64String(Encoding.GetEncoding("Unicode").GetBytes(model.Password));
            //                            string decrypted =  Encoding.GetEncoding("Unicode").GetString(Convert.FromBase64String(encrypted));
                            cookie.Values.Add(model.UserName, encrypted);
                            System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
                        }
                        else
                        {
                            string encrypted = Convert.ToBase64String(Encoding.GetEncoding("Unicode").GetBytes(model.Password));
            //                            string decrypted =  Encoding.GetEncoding("Unicode").GetString(Convert.FromBase64String(encrypted));
                            HttpCookie coolCookie = System.Web.HttpContext.Current.Response.Cookies["coolCookie"];
                            bool userPresent = false;
                            NameValueCollection nameValues = coolCookie.Values;
                            for (int i = 0; i < nameValues.Count; i++)
                            {
                                if (nameValues.GetKey(i).Equals(model.UserName))
                                {
                                    userPresent = true;
                                    break;
                                }
                            }

                            if (!userPresent)
                            {
                                NameValueCollection userPass = new NameValueCollection();
                                userPass.Set(model.UserName, encrypted);
                                System.Web.HttpContext.Current.Response.Cookies["coolCookie"].Values.Add(userPass);
                            }

                        }
                    }

                    ControllerContext.HttpContext.Session["loggedInUser"] = present.ElementAt(0);
                    return RedirectToAction("Index", "Statistics");
                }

                else if (present.Count == 0)
                {
                    ModelState.AddModelError("", "The user couldn't be found.");
                }

                else
                {
                    // bre
                }

            }

            // If we got this far, something failed, redisplay form
            //            return View(model);
            return View(model);
        }
Exemplo n.º 2
0
        public ActionResult LogOn( LogOnModel model, string returnUrl )
        {
            if ( ModelState.IsValid )
            {
                if ( Membership.ValidateUser( model.UserName, model.Password ) )
                {
                    FormsAuthentication.SetAuthCookie( model.UserName, model.RememberMe );
                    if ( Url.IsLocalUrl( returnUrl ) && returnUrl.Length > 1 && returnUrl.StartsWith( "/" )
                        && !returnUrl.StartsWith( "//" ) && !returnUrl.StartsWith( "/\\" ) )
                    {
                        return Redirect( returnUrl );
                    }
                    else
                    {
                        return RedirectToAction( "Index", "Home" );
                    }
                }
                else
                {
                    ModelState.AddModelError( "", "The user name or password provided is incorrect." );
                }
            }

            // If we got this far, something failed, redisplay form
            return View( model );
        }
Exemplo n.º 3
0
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (model.RememberMe)
                {
                    if (System.Web.HttpContext.Current.Response.Cookies["coolCookie"] == null)
                    {
                        HttpCookie cookie = new HttpCookie("coolCookie");
                        cookie.Values.Add(model.UserName, model.Password);
                        System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
                    }
                    else
                    {
                        HttpCookie coolCookie = System.Web.HttpContext.Current.Response.Cookies["coolCookie"];
                        bool userPresent = false;
                        NameValueCollection nameValues = coolCookie.Values;
                        for (int i = 0; i < nameValues.Count; i++)
                        {
                            if (nameValues.GetKey(i).Equals(model.UserName))
                            {
                                userPresent = true;
                                break;
                            }
                        }

                        if (!userPresent)
                        {
                            NameValueCollection userPass = new NameValueCollection();
                            userPass.Set(model.UserName, model.Password);
                            System.Web.HttpContext.Current.Response.Cookies["coolCookie"].Values.Add(userPass);
                        }

                    }
                }
                //if ( Url.IsLocalUrl( returnUrl ) && returnUrl.Length > 1 && returnUrl.StartsWith( "/" )
                //    && !returnUrl.StartsWith( "//" ) && !returnUrl.StartsWith( "/\\" ) )
                //{
                //    return Redirect( returnUrl );
                //}

                List<User> present = entities.Users.Where(x => x.Username == model.UserName).ToList();

                if (present.Count == 1)
                {
                    ControllerContext.HttpContext.Session["loggedInUser"] = present.ElementAt(0);
                    return RedirectToAction("Index", "Statistics");
                }

                else if (present.Count == 0)
                {
                    ModelState.AddModelError("", "The user couldn't be found.");
                }

                else
                {
                    // bre
                }

            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }