Ejemplo n.º 1
0
        public User GetUser(HttpRequestBase request)
        {
            User user = new User();

            HttpCookie myCookie = request.Cookies["BetBit"];
            if (myCookie == null)
            {

                var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
                var random = new Random();
                user.Username = new string(
                    Enumerable.Repeat(chars, 8)
                              .Select(s => s[random.Next(s.Length)])
                              .ToArray());

                user.Password = new string(
                    Enumerable.Repeat(chars, 12)
                              .Select(s => s[random.Next(s.Length)])
                              .ToArray());

                user.UserId = Guid.NewGuid();
                user.Balance = 0;

                myCookie = new HttpCookie("BetBit");
                myCookie.Value = user.UserId.ToString();
                myCookie.Expires = DateTime.Now.AddYears(1);
                Response.Cookies.Add(myCookie);

                BetBitEntities betBitEntities = new BetBitEntities();

                betBitEntities.Users.Add(new Users()
                {
                    UserId = user.UserId,
                    Username = user.Username,
                    Password = user.Password

                });

                betBitEntities.SaveChanges();
            }
            else
            {
                user.UserId = new Guid(myCookie.Value);

                BetBitEntities betBitEntities = new BetBitEntities();

                Users userFromBD = betBitEntities.Users.FirstOrDefault(i => i.UserId.Value.Equals(user.UserId));
                user.Username = userFromBD.Username;
                user.Password = userFromBD.Password;

                user.Balance = betBitEntities.Coupon.Where(i => i.UserId == user.UserId).Select(i => i.CouponAmount).DefaultIfEmpty(0).Sum();

            }
            return user;
        }
Ejemplo n.º 2
0
        public JsonResult Logout()
        {
            User user = new User();
            HttpCookie myCookie = null;

            var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

            var random = new Random();
            user.Username = new string(
                Enumerable.Repeat(chars, 8)
                          .Select(s => s[random.Next(s.Length)])
                          .ToArray());

            user.Password = new string(
                Enumerable.Repeat(chars, 12)
                          .Select(s => s[random.Next(s.Length)])
                          .ToArray());

            user.UserId = Guid.NewGuid();
            user.Balance = 0;

            myCookie = new HttpCookie("BetBit");
            myCookie.Value = user.UserId.ToString();
            myCookie.Expires = DateTime.Now.AddYears(1);
            Response.Cookies.Add(myCookie);

            BetBitEntities betBitEntities = new BetBitEntities();

            betBitEntities.Users.Add(new Users()
            {
                UserId = user.UserId,
                Username = user.Username,
                Password = user.Password,
                Balance = user.Balance
            });

            betBitEntities.SaveChanges();

            return Json(user, JsonRequestBehavior.AllowGet);
        }
Ejemplo n.º 3
0
        public JsonResult UserLogin(string username, string password, string logout)
        {
            BetBitEntities betBitEntities = new BetBitEntities();
            HttpCookie myCookie = Request.Cookies["BetBit"];
            User user = new User();

            var User = betBitEntities.Users.FirstOrDefault(i => i.Username.Equals(username) && i.Password.Equals(password));

            if (User != null)
            {
                myCookie = new HttpCookie("BetBit");
                myCookie.Value = User.UserId.ToString();
                myCookie.Expires = DateTime.Now.AddYears(1);
                Response.Cookies.Add(myCookie);

                user.Username = User.Username;
                user.Password = User.Password;
                user.UserId = User.UserId.Value;
            }
            if (logout.Equals("true"))
            {
                Response.Cookies["BetBit"].Expires = DateTime.Now.AddDays(-1);
                myCookie = null;

                if (myCookie == null)
                    return Logout();
            }
            return Json(user, JsonRequestBehavior.AllowGet);
        }