Exemplo n.º 1
0
        /// <summary>
        /// </summary>
        /// <param name="personId"></param>
        /// <param name="newAuthorization"></param>
        /// <param name="NewWellknownAuthorizationLevel"></param>
        /// <returns></returns>
        private async Task ModifySafeAut(Guid personId, Guid newAuthorization, WellknownAuthorizationLevel NewWellknownAuthorizationLevel)
        {
            try
            {
                if (await _context.Persons.AnyAsync(p => p.Id == personId))
                {
                    if (await AuthNotModified(personId))
                    {
                        return;
                    }
                    var user = await _context.Persons.FirstAsync(p => p.Id == personId);

                    if (user.SafeAuthModel == null)
                    {
                        return;
                    }
                    user.SafeAuthModel.AutId = newAuthorization;
                    var encryptedRelation = await _securLib.EncryptEntityRelation(user, user.AutorizationLevel);

                    user.SafeAuthModel.Control = await _securLib.EncriptLine(encryptedRelation);

                    await _context.SaveChangesAsync().ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                StaticEventHandler.Log(System.Diagnostics.TraceLevel.Error, "error during ModifySafeAut", MethodBase.GetCurrentMethod(), ex);
            }
        }
Exemplo n.º 2
0
        public async Task <bool> ChangeCardNumber(Guid personModelId, string CardNumber)
        {
            if (personModelId.Equals(Guid.Empty))
            {
                return(false);
            }
            if (string.IsNullOrWhiteSpace(CardNumber))
            {
                return(false);
            }
            try
            {
                if (!_context.Persons.Any(p => p.Id == personModelId))
                {
                    return(false);
                }
                if (!_context.CardModels.Any(c => c.CardNumber == CardNumber))
                {
                    return(false);
                }
                var usr = _context.Persons.First(p => p.Id == personModelId);
                usr.CardNumber = _context.CardModels.First(c => c.CardNumber == CardNumber);
                var encryptedRelation = await _securLib.EncryptEntityRelation(usr, usr.AutorizationLevel);

                usr.SafeAuthModel.Control = await _securLib.EncriptLine(encryptedRelation);

                _context.SaveChanges();
                return(true);
            }
            catch (Exception ex)
            {
                StaticEventHandler.Log(System.Diagnostics.TraceLevel.Error, "error during IsAutorized od person", MethodBase.GetCurrentMethod(), ex);
                return(false);
            }
        }
Exemplo n.º 3
0
        public async Task <PersonModel> Map(PersonCreateModel person)
        {
            var dbPersonId = Guid.NewGuid();
            var dbAuthId   = Guid.NewGuid();
            var dbPerson   = new PersonModel()
            {
                Id                = dbPersonId,
                LastModify        = DateTime.Now,
                Email             = person.Email,
                Name              = person.Name,
                Surname           = person.Surname,
                UserName          = person.UserName,
                Password          = person.Password,
                AutorizationLevel = new AutorizationLevelModel()
                {
                    Id             = Guid.NewGuid(),
                    CreationDate   = DateTime.UtcNow,
                    ExpirationDate = DateTime.UtcNow.AddDays(30),
                }
            };

            dbPerson.SafeAuthModel = new SafeAuthModel()
            {
                Id      = Guid.NewGuid(),
                AutId   = dbAuthId,
                UserId  = dbPersonId,
                Control = await _securLib.EncriptLine(await _securLib.EncryptEntityRelation(dbPerson, dbPerson.AutorizationLevel))
            };

            return(dbPerson);
        }
Exemplo n.º 4
0
        public async Task <SignInResult> ValidateLoginAsync(string username, string password, bool rememberMe, bool shouldLockout)
        {
            try
            {
                var userId = await GetUserByInputAsync(username).ConfigureAwait(false);

                if (userId.Equals(Guid.Empty))
                {
                    return(SignInResult.Failed);
                }
                if (await _context.Persons.AnyAsync(p => p.Id == userId).ConfigureAwait(false))
                {
                    PersonModel userSelected = await _context.Persons.Include(Auth => Auth.AutorizationLevel).Include(Card => Card.CardNumber).FirstOrDefaultAsync(p => p.Id == userId).ConfigureAwait(false);

                    var encrypted = await _securLib.EncriptLine(password);

                    if (await _securLib.CompareHash(userSelected.Password, encrypted))
                    {
                        // authentication successful so generate jwt token
                        var tokenHandler = new JwtSecurityTokenHandler();

                        var key             = Encoding.ASCII.GetBytes(_configuration["PreSharedKey"]);
                        var tokenDescriptor = new SecurityTokenDescriptor
                        {
                            Subject = new ClaimsIdentity(new Claim[]
                            {
                                new Claim(ClaimTypes.Name, userSelected.Id.ToString())
                            }),
                            Expires            = DateTime.UtcNow.AddDays(7),
                            SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
                        };
                        var token       = tokenHandler.CreateToken(tokenDescriptor);
                        var secureToken = tokenHandler.WriteToken(token);
                        return(SignInResult.Success);
                    }
                    else
                    {
                        return(SignInResult.Failed);
                    }
                }
                return(SignInResult.Failed);
            }
            catch (Exception ex)
            {
                StaticEventHandler.Log(System.Diagnostics.TraceLevel.Error, "error during Login", MethodBase.GetCurrentMethod(), ex);
                return(SignInResult.Failed);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// </summary>
        /// <returns></returns>
        public async Task CreateAdmin()
        {
            try
            {
                var card = new CardCreateModel()
                {
                    CardNumber     = "123456",
                    ExpirationDate = DateTime.UtcNow.AddYears(1),
                };
                var usr = new PersonCreateModel()
                {
                    UserName = "******",
                    Email    = "*****@*****.**",
                    Password = await _securLib.EncriptLine("Admin"),
                };
                var auth = new AutorizationLevelCreateModel()
                {
                    AuthName       = "ROOT",
                    AuthValue      = WellknownAuthorizationLevel.Root,
                    ExpirationDate = DateTime.UtcNow.AddYears(1)
                };
                await Create(usr, auth);

                var menu = _menuService.CreateMenuFromPages();
                foreach (var vm in menu)
                {
                    vm.AuthLevel = WellknownAuthorizationLevel.User;
                }
                _context.MainMenu.AddRange(menu);
                _context.SaveChanges();
            }
            catch (Exception ex)
            {
                StaticEventHandler.Log(System.Diagnostics.TraceLevel.Error, "error during IsAutorized of person", MethodBase.GetCurrentMethod(), ex);
            }
        }