Example #1
0
        public void Post([FromBody] UserModel user)
        {
            UserAccountBO userAccount = _mapper.Map <UserAccountBO>(user);

            userAccount.Role = UserRoleNames.USER;
            _userAccountService.AddUserAccount(userAccount);
        }
        public void AddUserAccount(UserAccountBO userAccount)
        {
            User user = _mapper.Map <User>(userAccount);

            user.RoleId = (int)UserRoles.USER;

            _userRepository.AddAsync(user);
        }
Example #3
0
        public async Task <ResponseBase <UserAccountBO> > AdminLogin([FromBody] LoginModel model)
        {
            if (!ModelState.IsValid)
            {
                return(new ErrorResponse <UserAccountBO>("Tüm zorunlu alanları doldurun", StatusCodes.Status401Unauthorized));
            }

            Task <UserAccountBO> userAccountTask = userAccountService.CheckUserAccount(model.Email, model.Password);
            UserAccountBO        userAccount     = await userAccountTask;

            if (userAccount == null)
            {
                return(new ErrorResponse <UserAccountBO>("Hatalı kullanıcı bilgileri", StatusCodes.Status401Unauthorized));
            }

            userAccount.Token = authenticationService.Authenticate(userAccount);

            return(new SuccessResponse <UserAccountBO>(userAccount));
        }
Example #4
0
        public string Authenticate(UserAccountBO userAccount)
        {
            var tokenHandler    = new JwtSecurityTokenHandler();
            var key             = Encoding.ASCII.GetBytes(ConfigGetter.GetSectionFromJson("SecretKey"));
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.Email, userAccount.Email),
                    new Claim(ClaimTypes.Role, UserRoleNames.GetRoleName(userAccount.RoleId))
                }),
                Expires            = DateTime.UtcNow.AddDays(Convert.ToInt32(ConfigGetter.GetSectionFromJson("TokenExpiresDay"))),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };
            var token    = tokenHandler.CreateToken(tokenDescriptor);
            var tokenStr = tokenHandler.WriteToken(token);

            return(tokenStr);
        }