public async Task <IActionResult> Index(LoginModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var userControl = await _userManager.FindByEmailAsync(model.Email);

                    if (userControl == null)
                    {
                        return(NotFound(new { Message = "Böyle bir kullanıcı bulunamadı!", IsSuccess = false }));
                    }

                    var result = await _signInManager.PasswordSignInAsync(userControl, model.Password, false, false);

                    if (result.Succeeded)
                    {
                        var tokenHandler = new TokenHandler(_configuration);
                        var user         = tokenHandler.CreateToken(model);
                        if (user != null)
                        {
                            return(Ok(user));
                        }
                        return(BadRequest(new { Message = "Token oluşturulamadı!", IsSuccess = false }));
                    }

                    return(BadRequest(new { Message = "Kullanıcı adı veya şifre hatalı!", IsSuccess = false }));
                }
                return(BadRequest(new { Message = "Hata oluştu!", IsSuccess = false }));
            }
            catch (Exception ex)
            {
                return(Ok(new { Message = $"Hata: {ex.Message}", IsSuccess = false }));
            }
        }
        /// <summary>
        ///Admin Login Async
        /// </summary>
        /// <param name="AdminLoginDTO"></param>
        /// <returns>bool</returns>
        public async Task <UserDTO> AdminLoginAsync(UserLoginDTO adminLoginDTO)
        {
            #region Declare a return type with initial value.
            UserDTO AdminReturn = new UserDTO();
            #endregion
            try
            {
                Admin admin = null;
                if (adminLoginDTO != null)
                {
                    admin = await UnitOfWork.AdminRepository.GetWhere(x => x.Email.Trim().ToLower().Equals(adminLoginDTO.Email.Trim().ToLower()) &&
                                                                      x.IsDeleted == (byte)DeleteStatusEnum.NotDeleted).FirstOrDefaultAsync();

                    if (admin != null)
                    {
                        if (!VerifyPasswordHash(adminLoginDTO.Password, admin.PasswordHash, admin.PasswordSalt))
                        {
                            AdminReturn = AdminMapping.MappingAdminToUserDTO(admin);
                        }

                        AdminReturn.Token = TokenHandler.CreateToken(AdminReturn).Token;
                    }
                }
            }
            catch (Exception exception)
            {
                // Logger.Instance.LogException(exception, LogLevel.Medium);
            }
            return(AdminReturn);
        }
Пример #3
0
        public IActionResult Login([FromBody] AgentLoginInputViewModel loginViewModel)
        {
            var agent = _agentRepository
                        .Find(a => a.BusinessId == loginViewModel.BusinessId && a.Password == loginViewModel.EncryptedPassword)
                        .FirstOrDefault();

            if (agent is null)
            {
                HandleError("Agent", "Invalid Business ID/password");
                return(Response(loginViewModel));
            }

            var userViewModel = new UserViewModel
            {
                Id       = agent.Id,
                UserType = UserType.Agent
            };
            var authResponse = new
            {
                token = TokenHandler.CreateToken(userViewModel, _configuration),
                agent = _mapper.Map <AgentViewModel>(agent)
            };

            return(Response(authResponse));
        }
        public User Authenticate(AuthenticateModel authenticateModel)
        {
            User user = getUser(authenticateModel, _usersContext);

            if (user == null)
            {
                return(null);
            }

            TokenHandler tokenHandler = new TokenHandler(_appSettings);

            user.Token = tokenHandler.CreateToken(user);

            return(user.WithoutPassword());
        }
Пример #5
0
        private string GenerateToken(Guid Id)
        {
            SecurityTokenDescriptor TokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.NameIdentifier, Id.ToString())
                }),
                Expires            = DateTime.UtcNow.AddDays(365),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(Encoding.ASCII.GetBytes(key)), SecurityAlgorithms.HmacSha256Signature)
            };
            SecurityToken Token = TokenHandler.CreateToken(TokenDescriptor);

            return(TokenHandler.WriteToken(Token));
        }
Пример #6
0
        public async Task <ActionResult> LogIn([FromBody] UserCredentials credentials)
        {
            try
            {
                var user = await _authRepository.AuthorizeAsync(credentials);

                if (user == null)
                {
                    return(NotFound());
                }

                var key = TokenHandler.CreateToken(user);
                return(new ObjectResult(new { token = key }));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(StatusCode(500));
            }
        }