コード例 #1
0
        public async Task <bool> LoginAsync(Login login)
        {
            var user = UserManager.Users.SingleOrDefault(u => u.UserName == login.UserName);

            if (user != null)
            {
                var result = await signInManager
                             .CheckPasswordSignInAsync(user, login.Password, false);

                if (result.Succeeded)
                {
                    await loginContext.RegisterLoginAsync(user);

                    return(true);
                }
                else if (result.IsLockedOut)
                {
                    modelValidator.AddError("Account has been locked out");
                }
                else if (result.IsNotAllowed)
                {
                    modelValidator.AddError("Invalid login");
                }
            }
            modelValidator.AddError("Login failed");
            return(false);
        }
コード例 #2
0
        public void NoModel()
        {
            var validator = new ModelValidator();

            validator.AddError("name", "what the hell?");
            validator.AddError("place", "when?");
            validator.AddError("name", "How?");

            Assert.Equal(2, validator["name"].Count());
            Assert.Single(validator["place"]);
        }
コード例 #3
0
        public void Modeled()
        {
            var model     = new List <int>();
            var validator = new ModelValidator();

            validator.AddError("name", "what the hell?");
            validator.AddError("place", "when?");
            validator.AddError(model, "name", "How?");

            Assert.Single(validator[model]);
            Assert.Single(validator[model, "name"]);
            Assert.Single(validator["name"]);
        }
コード例 #4
0
        public ClientPackage AddClient(ClientPackageAdd client)
        {
            var validator = modelValidator.GetHelper <ClientPackageAdd>();

            if (!context.Packages.Any(p => p.Id == client.PackageId && p.CreatedById == UserId))
            {
                modelValidator.AddError(ClientPackageAdd.InvalidPackage);
                return(null);
            }
            else if (context.ClientPackages.Any(c => c.PackageId == client.PackageId &&
                                                c.Source == client.Source))
            {
                validator.AddError(c => c.Source, ClientPackageAdd.DuplicateSource);
                return(null);
            }
            else
            {
                return(AddClient(client, UserId));
            }
        }
コード例 #5
0
        public async Task <bool> SignUpAsync(SignUp signUp)
        {
            var user = new User {
                UserName = signUp.UserName
            };
            var result = await userManager.CreateAsync(user, signUp.Password);

            if (result.Succeeded)
            {
                return(true);
            }
            if (result.Errors.Any())
            {
                foreach (var err in result.Errors)
                {
                    modelValidator.AddError(err.Description);
                }
            }
            else
            {
                modelValidator.AddError(UnknownErrorMessage);
            }
            return(false);
        }
コード例 #6
0
        public void NoModel()
        {
            var validator = new ModelValidator();
            var helper    = validator.GetHelper <Person>();

            helper.AddError(p => p.Name, "Name is too long");
            helper.AddError(p => p.Age, "You are getting too old for this");
            validator.AddError(nameof(Person.Age), "this one is personal");

            Assert.Equal(3, validator.GetErrors().SelectMany(e => e.Value).Count());
            Assert.Equal(2, validator.GetErrors().Count());
            Assert.Equal(2, validator.GetErrors(nameof(Person.Age)).Count());
            Assert.Single(validator.GetErrors(nameof(Person.Name)));
            Assert.Single(helper.GetErrors(p => p.Name));
        }
コード例 #7
0
        public void WithModel()
        {
            var model     = new Person();
            var validator = new ModelValidator();
            var helper    = validator.GetHelper(model);

            helper.AddError(p => p.Name, "Name is too long");
            helper.AddError(p => p.Age, "You are getting too old for this");
            validator.AddError(nameof(Person.Age), "this one is personal");

            Assert.Single(validator.GetErrors());
            Assert.Equal(2, validator.GetErrors(model).Count());
            Assert.Single(validator.GetErrors(model, nameof(Person.Age)));
            Assert.Single(helper.GetErrors(p => p.Age));
            Assert.Single(helper.GetErrors(p => p.Name));
            Assert.Null(helper.GetErrors(p => p.Hobby));
            Assert.Single(validator.GetErrors(model, nameof(Person.Name)));
        }