예제 #1
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
            }));
        }
        public ActionResult <TokenAndMessageReturn> SearchByIdNumber(string idNumber)
        {
            TokenAndMessageReturn tokenAndMessageReturn = null;

            if (idNumber == null)
            {
                return(BadRequest());
            }

            var visitor = _repo.SearchByIdNumber(idNumber);

            if (visitor == null)
            {
                tokenAndMessageReturn = new TokenAndMessageReturn
                {
                    Token   = null,
                    Message = "User Not Found",
                    VisitorInformationForReturn = null,
                    StatusCode = 404
                };

                return(NotFound(tokenAndMessageReturn));
            }
            var userBind = _repo.SearchInBindUser(idNumber, visitor.Id);

            if (userBind != null)
            {
                tokenAndMessageReturn = new TokenAndMessageReturn
                {
                    Token   = null,
                    Message = "User Is Blocked because" + userBind.Reason,
                    VisitorInformationForReturn = null,
                    StatusCode = 422
                };

                return(UnprocessableEntity(tokenAndMessageReturn));
            }
            else
            {
                var claims = new[]
                {
                    new Claim(JwtRegisteredClaimNames.Sub, idNumber),
                    new Claim(JwtRegisteredClaimNames.NameId, visitor.Id.ToString()),
                };

                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);

                tokenAndMessageReturn = new TokenAndMessageReturn
                {
                    Token   = tokenJson,
                    Message = "User Found",
                    VisitorInformationForReturn = new VisitorInformationForReturn
                    {
                        Name       = visitor.Name,
                        Email      = visitor.Email,
                        Governrate = visitor.Governrate,
                        Address    = visitor.Address,
                        IdNumber   = idNumber,
                        Image      = GetDirectoryPath.GetImagePath(Request, visitor.Image),
                        Phone      = visitor.Phone
                    },
                    StatusCode = 200
                };
                return(Ok(tokenAndMessageReturn));
            }
        }