Ejemplo n.º 1
0
        public async Task<ActionResult> Login(LogOnFormModel form, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                var user = this._userRepository.Get(x => x.Email.ToUpper() == form.Email.ToUpper() && Md5Encrypt.Md5EncryptPassword(form.Password) == x.PasswordHash);
                if (user != null)
                {
                    PointNetUser appUser = new PointNetUser(user);
                    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
                    AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = true, RedirectUri = returnUrl }, await appUser.GenerateUserIdentityAsync(_userManager));
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }

            return View(form);
        }
Ejemplo n.º 2
0
        public async Task<ActionResult> Register(UserFormModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var command = new UserRegisterCommand
                    {
                        FirstName = model.FirstName,
                        LastName = model.LastName,
                        Email = model.Email,
                        Password = model.Password,
                        Activated = true,
                        RoleId = (Int32)UserRoles.User
                    };

                    IEnumerable<ValidationResult> errors = _commandBus.Validate(command);
                    ModelState.AddModelErrors(errors);
                    if (ModelState.IsValid)
                    {
                        var result = _commandBus.Submit(command);
                        if (result.Success)
                        {
                            var user = this._userRepository.Get(x => x.Email.ToUpper() == command.Email.ToUpper() && Md5Encrypt.Md5EncryptPassword(command.Password) == x.PasswordHash);
                            PointNetUser appUser = new PointNetUser()
                            {
                                Id = user.UserId,
                                RoleName = Enum.GetName(typeof(UserRoles), user.RoleId),
                                UserName = user.DisplayName,
                                Email = user.Email
                            };
                            AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
                            AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = true }, await appUser.GenerateUserIdentityAsync(_userManager));
                            return RedirectToAction("Index", "Home");
                        }
                        else
                        {
                            ModelState.AddModelError("", "An unknown error occurred.");
                        }
                    }        
                }
                catch (Exception ex)
                {
                    _logger.Error(ex);
                    throw;
                }
                
                return View(model);
            }

            return View(model);
        }