Exemplo n.º 1
0
        public async Task <ActionResult> Login(LoginViewModel model)
        {
            if (string.IsNullOrEmpty(model.UserName) || string.IsNullOrEmpty(model.Password))
            {
                ModelState.AddModelError(string.Empty, ProjectResources.ResourceErrors.LoginError);
                return(Json(new
                {
                    success = false,
                    errors = ModelState.Keys.SelectMany(k => ModelState[k].Errors)
                             .Select(m => m.ErrorMessage).ToArray()
                }));
            }

            var response = await _userManager.CreateIdentity(model);

            if (response.IsSuccess)
            {
                _authManager.SignOut();
                _authManager.SignIn((ClaimsIdentity)response.Object, out var culture);
                CultureSetter.Set(culture, this);

                return(Json(new
                {
                    success = true
                }));
            }

            return(Json(new
            {
                success = false,
                errors = new string[] { ProjectResources.ResourceErrors.LoginError }
            }));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> UserProfile(UserProfileViewModel model)
        {
            var user = UserParser.UserProfileViewModelToUser(model);

            if (!string.IsNullOrEmpty(model.Password))
            {
                if ((string.IsNullOrEmpty(model.NewPassword) | string.IsNullOrEmpty(model.ConfirmNewPassword)) || !ModelState.IsValid)
                {
                    return(Json(new
                    {
                        success = false,
                        errors = ModelState.Keys.SelectMany(k => ModelState[k].Errors)
                                 .Select(m => m.ErrorMessage).ToArray()
                    }));
                }

                user.Password    = model.Password;
                user.NewPassword = model.NewPassword;
            }
            else
            {
                if (!ModelState.IsValid)
                {
                    return(Json(new
                    {
                        success = false,
                        errors = ModelState.Keys.SelectMany(k => ModelState[k].Errors)
                                 .Select(m => m.ErrorMessage).ToArray()
                    }));
                }
            }

            var response = await _userManager.Update(user, (ClaimsIdentity)this.User.Identity);

            if (response.IsSuccess)
            {
                CultureSetter.Set(model.Culture, this);

                return(Json(new
                {
                    success = true
                }));
            }

            if (response.Message.Equals("Wrong current password", StringComparison.OrdinalIgnoreCase))
            {
                return(Json(new
                {
                    success = false,
                    errors = new string[] { ProjectResources.ResourceErrors.ChangePasswordError }
                }));
            }

            return(Json(new
            {
                success = false,
                errors = new string[] { response.Message }
            }));
        }
Exemplo n.º 3
0
        public async Task <ActionResult> Registration(RegistrationViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(Json(new
                {
                    success = false,
                    errors = ModelState.Keys.SelectMany(k => ModelState[k].Errors)
                             .Select(m => m.ErrorMessage).ToArray()
                }));
            }

            var user     = UserParser.RegistrationViewModelToUser(model);
            var response = await _userManager.CreateIdentity(user);

            if (response.IsSuccess)
            {
                _authManager.SignIn((ClaimsIdentity)response.Object);
                CultureSetter.Set(model.Culture, this);

                return(Json(new
                {
                    success = true
                }));
            }

            string error = string.Empty;

            if (response.Message.Equals("Username is already taken", StringComparison.OrdinalIgnoreCase))
            {
                error = ProjectResources.ResourceErrors.UserNameIsTaken;
            }

            if (response.Message.Equals("Email is already taken", StringComparison.OrdinalIgnoreCase))
            {
                error = ProjectResources.ResourceErrors.EmailIsTaken;
            }


            return(Json(new
            {
                success = false,
                errors = new string[] { error }
            }));
        }
Exemplo n.º 4
0
        public ActionResult SetCulture(string culture)
        {
            CultureSetter.Set(culture, this);

            return(Redirect(Request.UrlReferrer.ToString()));
        }