Exemple #1
0
        public void PasswordFor_creates_required_data_attributes_for_string_with_Required_attribute()
        {
            // Arrange
            var htmlHelper = HtmlHelperFactory.Create(new RequiredStringModel());

            // Act
            var result = PasswordExtensions.PasswordFor(htmlHelper, exampleModel => exampleModel.String, true);

            // Assert
            Assert.AreEqual("<input " +
                            "data-msg-required=\"The String field is required.\" " +
                            "data-rule-required=\"true\" " +
                            "id=\"String\" name=\"String\" type=\"password\" />", result.ToHtmlString());
        }
        public async Task <ActionResult <PostTokenResponse> > PostToken([FromBody] PostTokenModel model)
        {
            var user = await _context.Users.Find(u => u.Username == model.Login).SingleOrDefaultAsync();

            if (user == default)
            {
                _logger.LogDebug("User {0} was not found", model.Login);
                return(NotFound());
            }

            var credential = await _context.Credentials.Find(c => c.Id == user.Id).SingleAsync();

            if (credential == default)
            {
                _logger.LogError("Credential for user {0} was not found", user.Id);
                return(StatusCode(500));
            }

            if (PasswordExtensions.HashPassword(model.Password, credential.Salt) != credential.PasswordHash)
            {
                _logger.LogDebug("Access denied for {0}", user.Id);
                return(StatusCode(403));
            }

            string token;

            do
            {
                token = PasswordGenerator.Generate(100);
            } while (await _context.Sessions.Find(token).AnyAsync());

            await _context.Sessions.InsertOneAsync(new Session
            {
                Token  = token,
                UserId = user.Id
            });

            return(new PostTokenResponse
            {
                Id = user.Id,
                Name = user.Name,
                Username = user.Username,
                Token = token,
                RegisterDate = user.RegisterDate,
                TotalAmountOfProducts = user.TotalAmountOfProducts
            });
        }
        private void AddDefaults(CancellationToken cancellationToken)
        {
            if (Teachers.Find(FilterDefinition <Teacher> .Empty).Any(cancellationToken))
            {
                return;
            }

            var teacher = new Teacher
            {
                Additional          = "",
                Email               = "",
                FirstName           = "Лариса",
                ImageUrl            = "",
                LastName            = "Мантулина",
                PhoneNumber         = "+380983989420",
                ReadGlobalStatistic = true,
                SecondName          = "Степановна",
                EditChildren        = true,
                EditTeachers        = true,
                EditSubjects        = true,
                PerHour             = 0m,
                IsOwner             = true,
                PerHourGroup        = 0m,
                Id = ObjectId.GenerateNewId().ToString()
            };

            Teachers.InsertOne(teacher, cancellationToken: cancellationToken);

            var credential = new Credential
            {
                TeacherId = teacher.Id,
                Salt      = PasswordGenerator.Generate(8)
            };

            credential.PasswordHash = PasswordExtensions.HashPassword("w5g5jCXn", credential.Salt);
            Credentials.InsertOne(credential, cancellationToken: cancellationToken);
        }