public Model_EmailConfirmation(string id)
 {
     var ctx = new DatabaseDataContext();
     string email = "";
     ctx.GetUserEmail(long.Parse(id), ref email);
     ConfirmHash = ctx.GetConfirmationHash(email).SingleOrDefault().strConfirmHash;
 }
        public Model_PasswordRequest(string id)
        {
            var IDs = id.Split(',');

            var ctx = new DatabaseDataContext();
            string email = "";
            ctx.GetUserEmail(long.Parse(IDs[0]), ref email);
            RecipientEmail = email;
            Password = IDs[1];
        }
 public GetUserProfileResult GetUserProfile(string token, string ipAddress)
 {
     Guid id = Guid.Empty;
     try
     {
       id = new Guid(token);
     }
     catch
     {
       throw new ApplicationException("60100 Supplied token could not be converted to a guid.");
     }
     var ctx = new DatabaseDataContext();
     var x = ctx._GetUserProfile(id, ipAddress);
     if ((int)x.ReturnValue == 0)
         throw new ApplicationException("60102 Get user profile failed.");
     return x.FirstOrDefault();
 }
Exemple #4
0
        public GetUserProfileResult GetUserProfile(string token, string ipAddress)
        {
            Guid id = Guid.Empty;

            try
            {
                id = new Guid(token);
            }
            catch
            {
                throw new ApplicationException("60100 Supplied token could not be converted to a guid.");
            }
            var ctx = new DatabaseDataContext();
            var x   = ctx._GetUserProfile(id, ipAddress);

            if ((int)x.ReturnValue == 0)
            {
                throw new ApplicationException("60102 Get user profile failed.");
            }
            return(x.FirstOrDefault());
        }
 public virtual ActionResult Logout()
 {
     var ctx = new DatabaseDataContext();
     try
     {
         ctx.LogoutUser(JAAPToken, HostIPAddress);
     }
     catch
     {
         // ignore the exception: 60030 Supplied token could not be converted to a guid.
     }
     // FormsAuthentication.SignOut();
     if (Response.Cookies != null)
     {
         var c = new HttpCookie("GameToken");
         c.Expires = DateTime.Now.AddYears(-1);
         HttpContext.Response.SetCookie(c);
     }
     return RedirectToAction(Index());
 }
 public virtual ActionResult Login(string language, string emailAddress, string password, bool rememberMe = false)
 {
     var ctx = new DatabaseDataContext();
     var token = "";
     var message = "";
     try
     {
         ctx.LoginUser(emailAddress, password, HostIPAddress);
     }
     catch (Exception e)
     {
         switch (e.ErrorCode())
         {
             case 60020:
                 message = Resources.Home.Login.LoginError_60020;
                 break;
             case 60022:
                 message = Resources.Home.Login.LoginError_60022;
                 break;
         }
     }
     return View();
 }
Exemple #7
0
 public virtual ActionResult Confirm(string reference)
 {
     var ctx = new DatabaseDataContext();
     ctx.ConfirmEmailAddress(reference);
     return View();
 }
Exemple #8
0
        public virtual ActionResult ResetPassword(string emailAddress)
        {
            try
            {
                emailAddress = emailAddress.Trim();
                try
                {
                    emailAddress = NormalizeEmailAddress(emailAddress);
                }
                catch
                {
                    throw new ApplicationException("60040 The supplied username is not a valid email address.");
                }
                long? userID = null;
                using (var ctx = new DatabaseDataContext())
                {
                    // find out language
                    userID = ctx.GetUserIDByEmail(emailAddress.ToLower());
                }
                if (!userID.HasValue)
                {
                    throw new ApplicationException("60041 User not found.");
                }

                try
                {
                    var password = Perpetuality.Utilities.ReadablePassword.GenerateReadablePassword();

                    // mail the new password
                    var client = new WebClient();
                    client.Encoding = Encoding.UTF8;
                    var body = client.DownloadString(ConfigurationManager.AppSettings["BaseURL"] + "/en/mail/?view=PasswordRequest&id=" + userID + "," + HttpUtility.UrlEncode(password));
                    var subject = client.ResponseHeaders["X-JaapMail-Subject"];
                    var recipient = client.ResponseHeaders["X-JaapMail-Recipient-Email"];
                    var name = client.ResponseHeaders["X-JaapMail-Recipient-Name"];
                    if (!string.IsNullOrWhiteSpace(name))
                    {
                        name = name.Replace("<", "");
                        name = name.Replace(">", "");
                    }
                    var error = client.ResponseHeaders["X-JaapMail-Error"];
                    SendMail(recipient, name, subject, body);

                    // update the database with the new password
                    using (var ctx = new DatabaseDataContext())
                    {
                        ctx.ChangeUserPasswordInternal(userID, password, true);
                   }
                }
                catch (Exception e)
                {
                    throw new ApplicationException("60043 Password retrieval failed.", e);
                }
            }
            catch (Exception e)
            {
                EventLogger.WriteEvent(e.Message, EventLogger.EventType.Error, "Perpetuality");
            }

            return View();
        }
Exemple #9
0
        public virtual JsonResult CalculatePlant(double longitude, double latitude, long plantTypeID, int size)
        {
            var result = new JsonResult();
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

            var ctx = new DatabaseDataContext();
            GamePrincipal user = null;
            try
            {
                user = HttpContext.User as GamePrincipal;
            }
            catch
            {
            }

            decimal? balance = null;
            decimal? creditProductionRate = null;
            DateTime? gameDate = null;
            decimal? installedPower = null;
            decimal? buildingCost = null;
            decimal? buildingPower = null;
            decimal? buildingRevenue = null;
            if (user != null)
            {
                var power = GetSolarPower(longitude, latitude);
                // call calculate plant
                ctx.InstallPlant(
                    (user.Identity as GameIdentity).UserID
                    , 1
                    , 1
                    , (decimal)longitude
                    , (decimal)latitude
                    , size
                    , (decimal)power
                    , true
                    , ref balance
                    , ref creditProductionRate
                    , ref gameDate
                    , ref installedPower
                    , ref buildingCost
                    , ref buildingPower
                    , ref buildingRevenue);

                result.Data = new {
                      balance = balance.Value
                    , rate = creditProductionRate.Value
                    , date = gameDate.Value
                    , power = installedPower.Value
                    , plant = new {
                            cost = buildingCost.Value
                        ,   power = buildingPower.Value
                        ,   revenue = buildingRevenue.Value
                        ,   sunpower = power
                    }
                };
            }
            else
            {
                // error
            }
            return result;
        }
        public virtual ActionResult ExternalLoginCallback(string returnUrl)
        {
            var ctx = new DatabaseDataContext();
            var bypass = ctx.GetSetting("ByPassPrefix");
            AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));
            if (!result.IsSuccessful)
            {
                return RedirectToAction("ExternalLoginFailure");
            }
            var email = result.ExtraData["username"].ToLower();
            long id = -1;
            try
            {
                id = ctx.GetUserIDByEmail(email);
            }
            catch
            {

            }

            if (id == -1)
            {
                // new user should not get a confirmation mail
                try
                {
                    id = ctx.RegisterNewUser(
                        email,
                        Convert.ToBase64String(new SHA1CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(new Guid().ToString()))),
                        false,
                        returnUrl
                    );
                }
                catch
                {
                }
            }

            string token = "";
            if (id != -1)
            {
                try
                {
                    token = ctx.LoginUser(bypass + "_" + email, "notneeded", MvcApplication.HostIPAddress(HttpContext));
                }
                catch (Exception exception)
                {
                    switch (exception.ErrorCode())
                    {
                        case 60021:
                            ctx.ConfirmEmailAddress(Convert.ToBase64String(new SHA1CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(email))));
                            break;
                    }
                }
                // second try
                if (string.IsNullOrWhiteSpace(token))
                {
                    try
                    {
                        token = ctx.LoginUser(bypass + "_" + email, "notneeded", MvcApplication.HostIPAddress(HttpContext));
                    }
                    catch
                    {
                    }
                }
            }

            if (!string.IsNullOrWhiteSpace(token))
            {
                var tokenCookie = new HttpCookie("GameToken", token);
                tokenCookie.Expires = DateTime.Now.AddYears(1);
                Response.Cookies.Add(tokenCookie);
                if (!string.IsNullOrWhiteSpace(returnUrl))
                {
                    if (returnUrl.Contains("{guid}"))
                    {
                        if (returnUrl.Contains("?"))
                            returnUrl = returnUrl.Replace("{guid}", "&guid=" + token);
                        else
                            returnUrl = returnUrl.Replace("{guid}", "?guid=" + token);
                    }
                }
                else
                {
                    returnUrl = Url.Action(MVC.Home.Index());
                }
                ViewBag.ReturnUrl = returnUrl;
                return View(MVC.Home.Views.Shared.ExternalLoginCallback);
            }

            return RedirectToAction("ExternalLoginFailure");
        }
Exemple #11
0
 public virtual ActionResult Profile()
 {
     var ctx = new DatabaseDataContext();
     var profile = ctx.GetUserProfile(JAAPToken , HostIPAddress);
     return View(new Profile(profile));
 }
Exemple #12
0
        public virtual ActionResult Login(string language, string emailAddress, string password, bool rememberMe = false)
        {
            var ctx = new DatabaseDataContext();
            var token = "";
            var message = "";
            try
            {
                token = ctx.LoginUser(emailAddress, password, HostIPAddress);

                if (!string.IsNullOrWhiteSpace(token))
                {
                    var tokenCookie = new HttpCookie("GameToken", token);
                    tokenCookie.Expires = DateTime.Now.AddYears(1);
                    Response.Cookies.Add(tokenCookie);
                }
            }
            catch (Exception e)
            {
                switch (e.ErrorCode())
                {
                    case 60020:
                        message = Resources.Home.Login.LoginError_60020;
                        break;
                    case 60022:
                        message = Resources.Home.Login.LoginError_60022;
                        break;
                }
            }
            return RedirectToAction(Index());
        }
 public int UpdateUserProfile(string token, string ipAddress, string name, string language)
 {
     Guid id = Guid.Empty;
     try
     {
         id = new Guid(token);
     }
     catch
     {
         throw new ApplicationException("60100 Supplied token could not be converted to a guid.");
     }
     var ctx = new DatabaseDataContext();
     var x = ctx._UpdateUserProfile(id, ipAddress, name, language);
     if (x == 0)
         throw new ApplicationException("60102 Get user profile failed.");
     return x;
 }
Exemple #14
0
 public virtual ActionResult Index(string view, string id)
 {
     var ctx = new DatabaseDataContext();
     ViewBag.DatabaseDataContext = ctx;
     return View(view, (object)id);
 }
Exemple #15
0
        public virtual JsonResult GetPowerPlants(long world, double minlon, double maxlon, double minlat, double maxlat)
        {
            var result = new JsonResult();
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

            var ctx = new DatabaseDataContext();
            GamePrincipal user = null;
            try
            {
                user = HttpContext.User as GamePrincipal;
            }
            catch
            {
            }

            var plants = ctx.GetWorldPlayerPlants((user.Identity as GameIdentity).UserID, world, (decimal)minlon, (decimal)maxlon, (decimal)minlat, (decimal)maxlat);
            result.Data = plants.Select(x => new { lon = x.numLongitude, lat = x.numLatitude, tp = x.intPowerPlantTypeID, id = x.autID }).ToList();
            return result;
        }
        public virtual ActionResult ResetPassword()
        {
            var ctx = new DatabaseDataContext();

            return View();
        }
        public virtual ActionResult ResetPassword(string emailAddress)
        {
            // find out language
            var ctx = new DatabaseDataContext();
            var id = ctx.GetUserIDByEmail(emailAddress.ToLower());

            var client = new WebClient();
            client.Encoding = Encoding.UTF8;
            var body = client.DownloadString(Request.Url.Host + "/en/mail/?view=EmailConfirmation&id=" + id.ToString());
            var subject = client.ResponseHeaders["X-JaapMail-Subject"];
            var recipient = client.ResponseHeaders["X-JaapMail-Recipient-Email"];
            var name = client.ResponseHeaders["X-JaapMail-Recipient-Name"];
            if (!string.IsNullOrWhiteSpace(name))
            {
                name = name.Replace("<", "");
                name = name.Replace(">", "");
            }
            var error = client.ResponseHeaders["X-JaapMail-Error"];
            SendMail(recipient, name, subject, body);

            return View();
        }
Exemple #18
0
 public virtual ActionResult Profile(Profile model)
 {
     var ctx = new DatabaseDataContext();
     ctx.UpdateUserProfile(JAAPToken, HostIPAddress, model.Name, model.Language);
     return View(model);
 }
Exemple #19
0
        public virtual ActionResult Register(string emailAddress, string password, string language)
        {
            try
            {
                long? userID = null;
                var userName = emailAddress.Trim();
                password = password.Trim();
                // validate the email address
                try
                {
                    userName = NormalizeEmailAddress(userName);
                }
                catch
                {
                    throw new ApplicationException("60001 The supplied username is not a valid email address.");
                }
                // validate the password
                if (string.IsNullOrEmpty(password) | password.Length < 6)
                {
                    throw new ApplicationException("60002 The supplied password is empty or too short.");
                }
                // store in DB
                var confirmationpwd = GenerateConfirmationHash(userName);
                using (var ctx = new DatabaseDataContext())
                {
                    if (ctx.RegisterNewUser(userName, password, confirmationpwd, false, ref userID) == 0)
                        throw new ApplicationException("60003 Registering new user failed.");
                    // send a confirmation mail
                    try
                    {
                        SendConfirmationMail(new MailAddress(userName), userID, language);
                    }
                    catch (Exception e)
                    {
                        throw new ApplicationException("60005 Sending confirmation mail failed.", e);
                    }
                }
                if (!userID.HasValue)
                    throw new ApplicationException("60005 Sending confirmation mail failed.");
                //return userID.Value;
            }
            catch (Exception e)
            {
                EventLogger.WriteEvent(e.Message, EventLogger.EventType.Error, "Perpetuality");
            }

            return View(Views.RegisterThanks);
        }
Exemple #20
0
        //
        // GET: /Game/
        public virtual ActionResult Index()
        {
            //
            var ctx = new DatabaseDataContext();
            GamePrincipal user = null;
            try
            {
                user = HttpContext.User as GamePrincipal;
            }
            catch
            {
            }
            decimal? balance = null;
            decimal? creditProductionRate = null;
            DateTime? gameDate = null;
            decimal? installedPower = null;
            if (user != null)
            {
                ctx.GetPlayerState((user.Identity as GameIdentity).UserID, 1, ref balance, ref creditProductionRate, ref gameDate, ref installedPower);
                // retrieve state
                ViewBag.PlayerState = new { balance = balance.Value, rate = creditProductionRate.Value, date = (gameDate.Value - new DateTime(1970, 1, 1)).TotalMilliseconds, power = installedPower.Value };

                return View();
            }
            else
            {
                ViewBag.PlayerState = new { balance = 3000000, rate = 0, date = new DateTime(2013, 4, 20), power = 0 };
                // retrieve state
                return View();
            }
        }