Example #1
0
        public bool Login(LoginViewModel loginViewModel)
        {
            var dal = new LoginViewModelValidator();

            dal.Validate(loginViewModel).AddToModelState(ModelState, null);
            return(ModelState.IsValid);
        }
Example #2
0
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            var validator = new LoginViewModelValidator();
            var result    = validator.Validate(this);

            return(result.Errors.Select(item => new ValidationResult(item.ErrorMessage, new[] { item.PropertyName })));
        }
Example #3
0
    public ActionResult Login([CustomizeValidator] LoginViewModel loginViewModel)
    {
        //ValidatorOptions.LanguageManager = new CustomLanguageManager();
        if (ModelState.IsValid)
        {
        }
        Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(string.Format("{0}-{1}", "zh", "CN"));

        Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentUICulture;
        //ValidatorOptions.LanguageManager = new LanguageManager() {Culture = Thread.CurrentThread.CurrentCulture};
        ValidatorOptions.LanguageManager.Enabled = true;
        ValidatorOptions.ResourceProviderType    = typeof(MyResources);

        var validator           = new LoginViewModelValidator();
        ValidationResult result = validator.Validate(loginViewModel);

        result.AddToModelState(ModelState, null);

        return(View(loginViewModel));
    }
        public async Task <ActionResult <ClientToken> > LoginAsync([FromBody] LoginViewModel loginUser)
        {
            LoginViewModelValidator loginViewModelValidator = new LoginViewModelValidator();
            var results = loginViewModelValidator.Validate(loginUser);

            if (!results.IsValid)
            {
                foreach (var failure in results.Errors)
                {
                    _logger.LogInformation("Property " + failure.PropertyName + " failed validation. Error was: " + failure.ErrorMessage);
                }
            }

            if (loginUser == null || !ModelState.IsValid || !results.IsValid)
            {
                return(BadRequest("user is not set."));
            }

            var user = await _usersService.FindUserAsync(loginUser.Username, loginUser.Password);

            if (user == null || !user.IsActive)
            {
                return(Unauthorized());
            }

            var result = await _tokenFactoryService.CreateJwtTokensAsync(user);

            await _tokenStoreService.AddUserTokenAsync(user, result.RefreshTokenSerial, result.AccessToken, null);

            await _uow.SaveChangesAsync();

            _antiforgery.RegenerateAntiForgeryCookies(result.Claims);

            return(Ok(new ClientToken {
                AccessToken = result.AccessToken, RefreshToken = result.RefreshToken
            }));
        }
Example #5
0
        public async Task <IActionResult> Login([FromForm] LoginViewModel model, string returnurl)
        {
            string redirectUrl = returnurl;

            if (string.IsNullOrEmpty(redirectUrl))
            {
                redirectUrl = SystemDefine.HomePage;
            }
            if (await _customerAppService.IsAuthenticated())
            {
                return(Redirect(redirectUrl));
            }
            var validator = LoginViewModelValidator.ValidateModel(model);

            if (validator.IsValid)
            {
                model = await _customerAppService.Login(model);
            }
            if (!model.IsSuccess && model.Messages.Count > 0)
            {
                foreach (var message in model.Messages)
                {
                    ModelState.AddModelError(string.Empty, message);
                }
            }
            if (model.IsSuccess)
            {
                ViewBag.RedirectUrl = redirectUrl;
            }
            if (!validator.IsValid)
            {
                validator.AddToModelState(ModelState, null, model);
            }
            ViewData["returnurl"] = redirectUrl;
            return(PartialView(model));
        }
 public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
 {
     var validator = new LoginViewModelValidator();
     var result = validator.Validate(this);
     return result.Errors.Select(item => new ValidationResult(item.ErrorMessage, new[] {item.PropertyName}));
 }
 public void SetUp()
 {
     _validator = new LoginViewModelValidator();
 }