Exemple #1
0
        public ActionResult Login(LoginViewModel model)
        {
            if (ModelState.IsValid)
            {
                using (var connection = new FCDBDataContext())
                {
                    var userAccount = connection.Accounts
                        .Where(account => account.Email == model.Email && account.Status == (int)UserAccountStatus.Active).ToList()
                        .FirstOrDefault(account => PasswordHash.PasswordHash.ValidatePassword(model.Password, account.PasswordHash));
                    
                    if (userAccount != null)
                    {
                        var connectionInfo = new ConnectionInfo(Guid.NewGuid()) { AccountId = userAccount.Id };

                        this.Session.SetConnectionInfo(connectionInfo);
                        ActiveConnections.Add(connectionInfo.ConnectionId, connectionInfo);

                        userAccount.LastLoggedOn = DateTime.Now;
                        userAccount.LastLoginIp = this.Request.UserHostAddress;

                        connection.SubmitChanges();
                        
                        return this.RedirectToAction("Index");
                    }
                    else
                    {
                        ModelState.AddModelError("", "Invalid email or password.");
                    }
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Exemple #2
0
        public ActionResult Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                using (var connection = new FCDBDataContext())
                {
                    if (connection.Accounts.Any(account => account.Email == model.Email))
                    {
                        ModelState.AddModelError("", "User with such email has been already regstered.");
                        return View(model);
                    }

                    string userIpAddress = this.Request.UserHostAddress;
                    if (connection.Accounts.Count(account => account.LastLoginIp == userIpAddress) > Constants.MaxNumberAccountsPerIp)
                    {
                        ModelState.AddModelError("", "Number of allowed users reached maximum.");
                        return View(model);
                    }

                    var newUserAccount = new Account
                    { 
                        Id = Guid.NewGuid(),
                        Email = model.Email,
                        PasswordHash = PasswordHash.PasswordHash.CreateHash(model.Password),
                        Status = (int)UserAccountStatus.Active,
                        Type = (int)UserAccountType.Player,
                        Money = 0,
                        CreatedOn = DateTime.Now,
                        LastLoggedOn = DateTime.Now,
                        LastLoginIp = userIpAddress
                    };

                    connection.Accounts.InsertOnSubmit(newUserAccount);
                    connection.SubmitChanges();

                    return this.RedirectToAction("Login");
                }
            }

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