//-------------------------OTHER FUNCTIONS-------------------------------------------------------------
        private async Task <AuthenticationResponse> BuildToken(CredentialsUser credentials)
        {
            var claims = new List <Claim>()
            {
                new Claim("email", credentials.email)
            };

            var user = await userManager.FindByEmailAsync(credentials.email);

            var claimsDB = await userManager.GetClaimsAsync(user);

            claims.AddRange(claimsDB);

            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["jwtKey"]));

            var crededntial = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            var expiration  = DateTime.UtcNow.AddDays(1);

            var token = new JwtSecurityToken(issuer: null, audience: null, claims: claims,
                                             expires: expiration, signingCredentials: crededntial);

            return(new AuthenticationResponse()
            {
                token = new JwtSecurityTokenHandler().WriteToken(token),
                expiration = expiration
            });
        }
Exemple #2
0
        public async Task <CredentialsUserDTO> GetCredentialsUser(int id)
        {
            CredentialsUser dbRecord = await _unitOfWork.CredentialsUserRepository.GetById(id);

            CredentialsUserDTO result = _mapper.Map <CredentialsUserDTO>(dbRecord);

            //result.Password = _qphEncriptador.Encriptar(result.Password);

            return(result);
        }
Exemple #3
0
        public async Task InsertCredentialsUser(CredentialsUserDTO newCredentialServer)
        {
            // Map to db record
            CredentialsUser dbRecord = _mapper.Map <CredentialsUser>(newCredentialServer);

            // Encrypt password
            //dbRecord.Password = _passwordService.Hash(dbRecord.Password);

            await _unitOfWork.CredentialsUserRepository.Add(dbRecord);

            await _unitOfWork.SaveAdministrationSwitchChangesAsync();
        }
        public async Task <ActionResult <AuthenticationResponse> > Login([FromBody] CredentialsUser credentials)
        {
            var result = await signInManager.PasswordSignInAsync(credentials.email, credentials.password,
                                                                 isPersistent : false, lockoutOnFailure : false);

            if (result.Succeeded)
            {
                return(await BuildToken(credentials));
            }
            else
            {
                return(BadRequest("Login Failed"));
            }
        }
        public async Task <ActionResult <AuthenticationResponse> > Add([FromBody] CredentialsUser credentials)
        {
            var user = new IdentityUser {
                UserName = credentials.email, Email = credentials.email
            };
            var result = await userManager.CreateAsync(user, credentials.password);

            if (result.Succeeded)
            {
                return(await BuildToken(credentials));
            }
            else
            {
                return(BadRequest(result.Errors));
            }
        }
Exemple #6
0
        public async Task <bool> UpdateCredentialsUser(CredentialsUserDTO updatedCredentialsUserDTO)
        {
            CredentialsUser existingRecord = await _unitOfWork.CredentialsUserRepository.GetById(updatedCredentialsUserDTO.Id);

            if (existingRecord == null)
            {
                throw new ValidationException("Registro no existe para el ID proporcionado.");
            }

            var updatedRecord = _mapper.Map <CredentialsUser>(updatedCredentialsUserDTO);

            _unitOfWork.CredentialsUserRepository.Update(existingRecord, updatedRecord);

            await _unitOfWork.SaveAdministrationSwitchChangesAsync();

            return(true);
        }
Exemple #7
0
            public static CredentialsUser GetFromXElement(XContainer groupElement)
            {
                if (groupElement == null)
                {
                    return(null);
                }

                XElement nameElement = groupElement.Element("Name");

                if (nameElement == null)
                {
                    return(null);
                }

                string name = nameElement.Value.Trim().ToLower();

                if (name.Length == 0)
                {
                    return(null);
                }

                List <XElement> rightElements = groupElement.Descendants("Right").ToList();

                if (rightElements.Count == 0)
                {
                    return(null);
                }

                List <string> rightnames = rightElements.ConvertAll(element => element.Value.Trim());

                CredentialsUser result = new CredentialsUser
                {
                    Name   = name,
                    Rights = new HashSet <string>(rightnames.FindAll(rightname => rightname.Trim().Length > 0).ConvertAll(rightname => rightname.ToLower()))
                };

                return((result.Rights.Count == 0) ? null : result);
            }
Exemple #8
0
        public static Dictionary <string, HashSet <string> > ReadCredentials(string filePathOrFileContent, bool isFilePath)
        {
            if (isFilePath && !File.Exists(filePathOrFileContent))
            {
                return(new Dictionary <string, HashSet <string> >());
            }

            if (isFilePath)
            {
                Util.WaitUntilReadable(filePathOrFileContent, 10000);
            }

            XDocument settingsFile = isFilePath ? XDocument.Load(filePathOrFileContent) : XDocument.Parse(filePathOrFileContent);

            if (settingsFile.Root == null || settingsFile.Root.Name != "Settings")
            {
                return(new Dictionary <string, HashSet <string> >());
            }

            Dictionary <string, HashSet <string> > result = new Dictionary <string, HashSet <string> >();

            foreach (XElement groupElement in settingsFile.Root.Descendants("Group"))
            {
                CredentialsGroup group = CredentialsGroup.GetFromXElement(groupElement);
                if (group == null)
                {
                    continue;
                }

                foreach (string username in group.Users)
                {
                    if (result.ContainsKey(username))
                    {
                        result[username].UnionWith(group.Rights);
                    }
                    else
                    {
                        result[username] = new HashSet <string>(group.Rights);
                    }
                }
            }

            XElement usersElement = settingsFile.Root.Element("Users");

            if (usersElement != null)
            {
                foreach (XElement userElement in usersElement.Elements("User"))
                {
                    CredentialsUser user = CredentialsUser.GetFromXElement(userElement);
                    if (user == null)
                    {
                        continue;
                    }

                    if (result.ContainsKey(user.Name))
                    {
                        result[user.Name].UnionWith(user.Rights);
                    }
                    else
                    {
                        result[user.Name] = new HashSet <string>(user.Rights);
                    }
                }
            }

            return(result);
        }