Esempio n. 1
0
        private ClaimsIdentity GetIdentity(string username, string password)
        {
            var    hash        = new HashPasswordOprions(password);
            string userPasHash = hash.GetHashString();

            var user = _context.Users.FirstOrDefault(u => u.UserName == username && u.Password == userPasHash);

            if (user != null)
            {
                var claims = new List <Claim>
                {
                    new Claim(ClaimsIdentity.DefaultNameClaimType, user.UserName),
                    new Claim(ClaimsIdentity.DefaultRoleClaimType, user.UserRole),
                };



                ClaimsIdentity claimsIdentity = new ClaimsIdentity(
                    claims,
                    "Token",
                    ClaimsIdentity.DefaultNameClaimType,
                    ClaimsIdentity.DefaultRoleClaimType);
                return(claimsIdentity);
            }
            return(null);
        }
        public async Task <ActionResult <Worker> > PostWorker(Worker worker)
        {
            var    hash        = new HashPasswordOprions(worker.Password);
            string userPasHash = hash.GetHashString();

            _context.Workers.Add(new Worker
            {
                FirstName    = worker.FirstName,
                LastName     = worker.LastName,
                Patronymic   = worker.Patronymic,
                UserName     = worker.UserName,
                Password     = userPasHash,
                Phone        = worker.Phone,
                UserRole     = "worker",
                DepartmentId = worker.DepartmentId,
                PositionId   = worker.PositionId,
                WorkPhone    = worker.WorkPhone,
                Id           = Guid.NewGuid()
            });
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (WorkerExists(worker.Id))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtAction("GetWorker", new { id = worker.Id }, worker));
        }
        public async Task <ActionResult <Client> > PostClient(Client client)
        {
            var    hash        = new HashPasswordOprions(client.Password);
            string userPasHash = hash.GetHashString();

            _context.Clients.Add(new Client
            {
                FirstName  = client.FirstName,
                LastName   = client.LastName,
                Patronymic = client.Patronymic,
                UserName   = client.UserName,
                Password   = userPasHash,
                Phone      = client.Phone,
                UserRole   = "client",
                Id         = Guid.NewGuid(),
                DiscountId = client.DiscountId,
                Address    = client.Address,
            });
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (ClientExists(client.Id))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtAction("GetClient", new { id = client.Id }, client));
        }