public async Task <SpartaUser> Create(SpartaUser user, string password)
        {
            if (string.IsNullOrWhiteSpace(password))
            {
                throw new ArgumentNullException("Lozinka je obavezna.");
            }
            var userExists = _uow.GetREpository <SpartaUser>().Any(x => x.UserName == user.UserName);

            if (userExists)
            {
                throw new ArgumentNullException("Korisničko ime \"" + user.UserName + "\" već postoji.");
            }

            byte[] passwordHash, passwordSalt;
            CreatePasswordHash(password, out passwordHash, out passwordSalt);

            user.PasswordHash = passwordHash;
            user.PasswordSalt = passwordSalt;

            try
            {
                _uow.GetREpository <SpartaUser>().Insert(user);
                await _uow.SaveChangesAsync();

                return(user);
            }
            catch
            {
                throw;
            }
        }
Example #2
0
 public AuthenticateResponse(SpartaUser user, string token)
 {
     Id        = user.Id;
     FirstName = user.FirstName;
     LastName  = user.LastName;
     UserName  = user.UserName;
     Token     = token;
 }
        public async Task Update(SpartaUser userRequest, string password = null)
        {
            var user = await _uow.GetREpository <SpartaUser>().GetById(userRequest.Id);

            if (user == null)
            {
                throw new ArgumentNullException("Empty");
            }

            if (!string.IsNullOrWhiteSpace(userRequest.UserName) && userRequest.UserName != user.UserName)
            {
                var userExists = _uow.GetREpository <SpartaUser>().Any(x => x.UserName == userRequest.UserName);
                if (userExists)
                {
                    throw new ArgumentNullException("Username " + userRequest.UserName + " is already taken");
                }

                user.UserName = userRequest.UserName;
            }

            if (!string.IsNullOrWhiteSpace(userRequest.FirstName))
            {
                user.FirstName = userRequest.FirstName;
            }

            if (!string.IsNullOrWhiteSpace(userRequest.LastName))
            {
                user.LastName = userRequest.LastName;
            }

            if (!string.IsNullOrWhiteSpace(userRequest.Unit))
            {
                user.Unit = userRequest.Unit;
            }

            if (!string.IsNullOrWhiteSpace(userRequest.Title))
            {
                user.Title = userRequest.Title;
            }

            if (!string.IsNullOrWhiteSpace(userRequest.Role))
            {
                user.Role = userRequest.Role;
            }

            if (!string.IsNullOrWhiteSpace(password))
            {
                byte[] passwordHash, passwordSalt;
                CreatePasswordHash(password, out passwordHash, out passwordSalt);

                user.PasswordHash = passwordHash;
                user.PasswordSalt = passwordSalt;
            }

            _uow.GetREpository <SpartaUser>().Update(user);
            await _uow.SaveChangesAsync();
        }
        private string GenerateJwtToken(SpartaUser user)
        {
            var tokenHandler    = new JwtSecurityTokenHandler();
            var key             = System.Text.Encoding.ASCII.GetBytes(_appSettings.Secret);
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject            = new ClaimsIdentity(new[] { new Claim("id", user.Id.ToString()) }),
                Expires            = DateTime.UtcNow.AddHours(8),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };
            var token = tokenHandler.CreateToken(tokenDescriptor);

            return(tokenHandler.WriteToken(token));
        }