Пример #1
0
 private void AddDefaultUser()
 {
     AddUser(CommonConstants.DefaultUserUsername,
             CommonConstants.DefaultClientCertificateThumbprint,
             CommonConstants.DefaultClientCrtCertificateName,
             HashComputer.ComputePasswordHash(CommonConstants.DefaultUserUsername), true);
 }
        public async Task <IActionResult> Registrate([FromForm] RegistrationViewModel model)
        {
            RegistrationValidator validator = new RegistrationValidator(_userManager);
            var results = validator.Validate(model);

            if (!results.IsValid)
            {
                TempData[TextConstants.TempDataErrorText] = ValidatorHelper.GetErrorString(results.Errors);
                return(View("Registration", model));
            }

            List <KeyValuePair <string, ProductRoleEnum> > products = null;

            if (!string.IsNullOrEmpty(model.ProductKey) && !string.IsNullOrEmpty(model.Role))
            {
                products = new List <KeyValuePair <string, ProductRoleEnum> >()
                {
                    new KeyValuePair <string, ProductRoleEnum>(model.ProductKey,
                                                               (ProductRoleEnum)Int32.Parse(model.Role))
                };
            }

            _userManager.AddUser(model.Username, null, null,
                                 HashComputer.ComputePasswordHash(model.Password), false, products);
            await Authenticate(model.Username, true);

            if (!string.IsNullOrEmpty(model.TicketId))
            {
                _ticketManager.RemoveTicket(Guid.Parse(model.TicketId));
            }

            return(RedirectToAction("Index", "Home"));
        }
Пример #3
0
        public User Authenticate(string login, string password)
        {
            var passwordHash = HashComputer.ComputePasswordHash(password);
            var existingUser = Users.SingleOrDefault(u => u.UserName.Equals(login) && !string.IsNullOrEmpty(u.Password) && u.Password.Equals(passwordHash));

            //var existingUser = _userManager.Users.SingleOrDefault(u => u.UserName.Equals(login));

            return(existingUser?.WithoutPassword());
        }
Пример #4
0
        public User Authenticate(string login, string password)
        {
            var passwordHash = HashComputer.ComputePasswordHash(password);

            bool IsAskedUser(KeyValuePair <Guid, User> userPair)
            {
                var user = userPair.Value;

                return(user.UserName.Equals(login) && !string.IsNullOrEmpty(user.Password) && user.Password.Equals(passwordHash));
            }

            var existingUser = _users.SingleOrDefault(IsAskedUser);

            return(existingUser.Value?.WithoutPassword());
        }
        public void CreateUser([FromBody] UserViewModel model)
        {
            UserValidator validator = new UserValidator(_userManager);
            var           results   = validator.Validate(model);

            if (!results.IsValid)
            {
                TempData[TextConstants.TempDataErrorText] = ValidatorHelper.GetErrorString(results.Errors);
            }

            else
            {
                _userManager.AddUser(model.Username, string.Empty, string.Empty,
                                     HashComputer.ComputePasswordHash(model.Password), model.IsAdmin);
            }
        }
        public (X509Certificate2, X509Certificate2) SignClientCertificate(User user, string subject, string commonName,
                                                                          RSAParameters rsaParameters)
        {
            (X509Certificate2, X509Certificate2)result;
            var rsa = RSA.Create(rsaParameters);

            X509Certificate2 clientCert =
                CertificatesProcessor.CreateAndSignCertificate(subject, rsa, CertificatesConfig.CACertificate);

            string fileName = $"{commonName}.crt";

            _certificateManager.InstallClientCertificate(clientCert);
            _certificateManager.SaveClientCertificate(clientCert, fileName);
            _userManager.AddUser(commonName, clientCert.Thumbprint, fileName,
                                 HashComputer.ComputePasswordHash(commonName), true);
            result.Item1 = clientCert;
            result.Item2 = CertificatesConfig.CACertificate;
            return(result);
        }
Пример #7
0
        private void MigrateUsersToDatabase()
        {
            List <User> usersFromFile = ParseUsersFile();

            foreach (var user in usersFromFile)
            {
                if (user.UserName.Equals("default"))
                {
                    user.IsAdmin = true;
                }

                if (string.IsNullOrEmpty(user.Password))
                {
                    AddUser(user.UserName, user.CertificateThumbprint, user.CertificateFileName,
                            HashComputer.ComputePasswordHash(user.UserName), user.IsAdmin);
                }
            }

            _logger.LogInformation($"{usersFromFile.Count} successfully migrated from file to database");
        }
Пример #8
0
 public User CreateThirdUser()
 {
     return(CreateUser(ThirdUserName, HashComputer.ComputePasswordHash(ThirdUserName)));
 }
Пример #9
0
 public User CreateSecondUser()
 {
     return(CreateUser(SecondUserName, HashComputer.ComputePasswordHash(SecondUserName)));
 }
Пример #10
0
 public User CreateFirstUser()
 {
     return(CreateUser(FirstUserName, HashComputer.ComputePasswordHash(FirstUserName)));
 }