Ejemplo n.º 1
0
        public UserData(string userName, bool validateUserExists = false)
        {
            System.Diagnostics.Debug.Print("UserData({0}, {1})", userName, validateUserExists.ToString());

            var val = UserDefinition.Parse(userName);

            if (val == null)
            {
                throw new ArgumentException("UserName does not meet expectations");
            }

            if (validateUserExists)
            {
                VoatUser user = null;
                if (!String.IsNullOrWhiteSpace(userName))
                {
                    using (var repo = new UserManager <VoatUser>(new UserStore <VoatUser>(new ApplicationDbContext())))
                    {
                        user = repo.FindByName(userName);
                    }
                }
                if (user == null)
                {
                    throw new VoatNotFoundException("User doesn't exist");
                }
            }
            this._userName = userName;
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (Settings.RegistrationDisabled)
            {
                return(View("RegistrationDisabled"));
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            if (!Utilities.AccountSecurity.IsPasswordComplex(model.Password, model.UserName, false))
            {
                ModelState.AddModelError(string.Empty, "Your password is not secure. You must use at least one uppercase letter, one lowercase letter, one number and one special character such as ?, ! or .");
                return(View(model));
            }

            try
            {
                // get user IP address
                string clientIpAddress = UserHelper.UserIpAddress(Request);

                // check the number of accounts already in database with this IP address, if number is higher than max conf, refuse registration request
                var accountsWithSameIp = UserManager.Users.Count(x => x.LastLoginFromIp == clientIpAddress);
                if (accountsWithSameIp >= Settings.MaxAllowedAccountsFromSingleIP)
                {
                    ModelState.AddModelError(string.Empty, "This device can not be used to create a voat account.");
                    return(View(model));
                }

                var user = new VoatUser
                {
                    UserName             = model.UserName,
                    RegistrationDateTime = Repository.CurrentDate,
                    LastLoginFromIp      = clientIpAddress,
                    LastLoginDateTime    = Repository.CurrentDate
                };

                // try to create new user account
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await SignInAsync(user, isPersistent : false);

                    // redirect new users to Welcome actionresult
                    return(RedirectToAction("Welcome", "Home"));
                }
                AddErrors(result);
            }
            catch (Exception)
            {
                ModelState.AddModelError(string.Empty, "Something bad happened. You broke Voat.");
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Ejemplo n.º 3
0
        private async Task SignInAsync(VoatUser user, bool isPersistent)
        {
            AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
            var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);

            AuthenticationManager.SignIn(new AuthenticationProperties {
                IsPersistent = isPersistent
            }, identity);
        }
Ejemplo n.º 4
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            try
            {
                // get user IP address
                string clientIpAddress = Utils.User.UserIpAddress(Request);

                // check the number of accounts already in database with this IP address, if number is higher than max conf, refuse registration request
                var accountsWithSameIp = UserManager.Users.Count(x => x.LastLoginFromIp == clientIpAddress);
                if (accountsWithSameIp >= MvcApplication.MaxAllowedAccountsFromSingleIP)
                {
                    ModelState.AddModelError(string.Empty, "This device can not be used to create a voat account.");
                    return(View(model));
                }

                var user = new VoatUser
                {
                    UserName             = model.UserName,
                    RegistrationDateTime = DateTime.Now,
                    LastLoginFromIp      = clientIpAddress,
                    LastLoginDateTime    = DateTime.Now
                };

                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await SignInAsync(user, isPersistent : false);

                    // redirect new users to Welcome actionresult
                    return(RedirectToAction("Welcome", "Home"));
                }
                AddErrors(result);
            }
            catch (Exception)
            {
                ModelState.AddModelError(string.Empty, "Something bad happened. You broke Whoaverse.");
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Ejemplo n.º 5
0
        public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Manage"));
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    return(View("ExternalLoginFailure"));
                }
                var user = new VoatUser {
                    UserName = model.UserName
                };
                var result = await UserManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);

                    if (result.Succeeded)
                    {
                        await SignInAsync(user, isPersistent : false);

                        return(RedirectToLocal(returnUrl));
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return(View(model));
        }
Ejemplo n.º 6
0
 private async Task SignInAsync(VoatUser user, bool isPersistent)
 {
     AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
     var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
     AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, identity);
 }
Ejemplo n.º 7
0
        public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();
                if (info == null)
                {
                    return View("ExternalLoginFailure");
                }
                var user = new VoatUser { UserName = model.UserName };
                var result = await UserManager.CreateAsync(user);
                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);
                    if (result.Succeeded)
                    {
                        await SignInAsync(user, isPersistent: false);
                        return RedirectToLocal(returnUrl);
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
Ejemplo n.º 8
0
        public async Task<ActionResult> Register(RegisterViewModel model)
        {

            if (Settings.RegistrationDisabled)
            {
                return View("RegistrationDisabled");
            }

            if (!ModelState.IsValid) return View(model);

            if (!Business.Utilities.AccountSecurity.IsPasswordComplex(model.Password, model.UserName))
            {
                ModelState.AddModelError(string.Empty, "Your password is not secure. You must use at least one uppercase letter, one lowercase letter, one number and one special character such as ?, ! or .");
                return View(model);
            }

            try
            {
                // get user IP address
                string clientIpAddress = UserHelper.UserIpAddress(Request);

                // check the number of accounts already in database with this IP address, if number is higher than max conf, refuse registration request
                var accountsWithSameIp = UserManager.Users.Count(x => x.LastLoginFromIp == clientIpAddress);
                if (accountsWithSameIp >= Settings.MaxAllowedAccountsFromSingleIP)
                {
                    ModelState.AddModelError(string.Empty, "This device can not be used to create a voat account.");
                    return View(model);
                }

                var user = new VoatUser
                {
                    UserName = model.UserName, 
                    RegistrationDateTime = DateTime.Now,
                    LastLoginFromIp = clientIpAddress,
                    LastLoginDateTime = DateTime.Now
                };

                // try to create new user account
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await SignInAsync(user, isPersistent: false);

                    // redirect new users to Welcome actionresult
                    return RedirectToAction("Welcome", "Home");
                }
                AddErrors(result);
            }
            catch (Exception)
            {
                ModelState.AddModelError(string.Empty, "Something bad happened. You broke Voat.");
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Ejemplo n.º 9
0
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (!ModelState.IsValid) return View(model);

            try
            {
                // get user IP address
                string clientIpAddress = Utils.User.UserIpAddress(Request);

                // check the number of accounts already in database with this IP address, if number is higher than max conf, refuse registration request
                var accountsWithSameIp = UserManager.Users.Count(x => x.LastLoginFromIp == clientIpAddress);
                if (accountsWithSameIp >= MvcApplication.MaxAllowedAccountsFromSingleIP)
                {
                    ModelState.AddModelError(string.Empty, "This device can not be used to create a voat account.");
                    return View(model);
                }

                var user = new VoatUser
                {
                    UserName = model.UserName, 
                    RegistrationDateTime = DateTime.Now,
                    LastLoginFromIp = clientIpAddress,
                    LastLoginDateTime = DateTime.Now
                };

                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInAsync(user, isPersistent: false);

                    // redirect new users to Welcome actionresult
                    return RedirectToAction("Welcome", "Home");
                }
                AddErrors(result);
            }
            catch (Exception)
            {
                ModelState.AddModelError(string.Empty, "Something bad happened. You broke Whoaverse.");
            }

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