Beispiel #1
0
        public VisitorInformation AddVisitorInformation(VisitorInformationForCreation visitorInformationViewModel)
        {
            var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/NationalIdImages");
            var fileName   = string.Empty;

            if (visitorInformationViewModel.Image != null)
            {
                fileName = visitorInformationViewModel.Image.FileName;
                var fullPath = Path.Combine(pathToSave, fileName);

                using (var stream = new FileStream(fullPath, FileMode.Create))
                {
                    visitorInformationViewModel.Image.CopyTo(stream);
                }
            }

            var visitorInformation = new VisitorInformation
            {
                Name       = visitorInformationViewModel.Name,
                Address    = visitorInformationViewModel.Address,
                Email      = visitorInformationViewModel.Email,
                Governrate = visitorInformationViewModel.Governrate,
                IdNumber   = visitorInformationViewModel.IdNumber,
                Phone      = visitorInformationViewModel.Phone,
                Image      = fileName
            };

            _db.VisitorInformations.Add(visitorInformation);
            _db.SaveChanges();

            return(visitorInformation);
        }
Beispiel #2
0
        public ActionResult <TokenAndMessageReturn> PostVisitorInformation([FromForm] VisitorInformationForCreation visitorInformationViewModel)
        {
            if (visitorInformationViewModel == null)
            {
                return(BadRequest(new TokenAndMessageReturn
                {
                    Token = null,
                    Message = "User Information Not Corrected Or Empty",
                    VisitorInformationForReturn = null,
                    StatusCode = 400
                }));
            }
            var visitorInformation = _repo.AddVisitorInformation(visitorInformationViewModel);
            var claims             = new[]
            {
                new Claim(JwtRegisteredClaimNames.Sub, visitorInformationViewModel.IdNumber),
            };

            var secretBytes = Encoding.UTF8.GetBytes(_config["Jwt:secret"]);
            var key         = new SymmetricSecurityKey(secretBytes);
            var algorithm   = SecurityAlgorithms.HmacSha256;

            var signingCredentials = new SigningCredentials(key, algorithm);

            var token = new JwtSecurityToken(
                _config["Jwt:Issuer"],
                _config["Jwt:Audience"],
                claims,
                notBefore: DateTime.Now,
                expires: DateTime.Now.AddHours(1),
                signingCredentials);

            var tokenJson = new JwtSecurityTokenHandler().WriteToken(token);

            return(Ok(new TokenAndMessageReturn
            {
                Token = tokenJson,
                Message = "User Has Been Created Successfully",
                VisitorInformationForReturn = new VisitorInformationForReturn
                {
                    Email = visitorInformation.Email,
                    Governrate = visitorInformation.Governrate,
                    Address = visitorInformation.Address,
                    Phone = visitorInformation.Phone,
                    Name = visitorInformation.Name,
                    Image = GetDirectoryPath.GetImagePath(Request, visitorInformation.Image),
                    IdNumber = visitorInformation.IdNumber
                },
                StatusCode = 201
            }));
        }