public async Task LoginAsync(UserCredentialDTO userCredentialModel, CancellationToken cancellationToken)
        {
            var identityServer = await DiscoveryClient.GetAsync(ApiSettings.IdentityServiceUrl);

            if (identityServer.IsError)
            {
                throw new AuthenticationException(Strings.NotConnectToIdentity);
            }

            var authClient = new TokenClient(
                identityServer.TokenEndpoint,
                ApiSettings.ClientId,
                ApiSettings.ClientSecret);

            var userTokenResponse = await authClient.RequestResourceOwnerPasswordAsync(
                userCredentialModel.Login,
                userCredentialModel.Password,
                ApiSettings.Scope,
                cancellationToken : cancellationToken);

            if (userTokenResponse.IsError || userTokenResponse.AccessToken == null)
            {
                throw new AuthenticationException(Strings.InitializeTokeException);
            }

            await _secureStorage.SetAsync(_tokenKey, userTokenResponse.AccessToken);
        }
 public IActionResult Register([FromBody] UserCredentialDTO dto)
 {
     return(this.Created(() =>
                         _UserRepo.Add(dto).Equals((int)TransactionStatus.SUCCESS)
             ? dto.ToJson()
             : new JObject()
                         ));
 }
        public int Add(UserCredentialDTO dto)
        {
            User user = _Mapper.Map <UserCredentialDTO, User>(dto);

            user.SaltToken = Guid.NewGuid().ToString().Replace("-", "").ToLower();
            user.Password  = CredentialHelper.CreatePasswordHash(user.Password, user.SaltToken);

            return(Add(user));
        }
        public int UpdateCredentials(int UserId, UserCredentialDTO dto)
        {
            User user = Find(i => i.Id.Equals(UserId));

            if (user is null || user.Id <= 0)
            {
                return((int)TransactionStatus.ENTITY_NOT_FOUND);
            }

            return(Update(user, dto));
        }
Exemple #5
0
        public async Task <object> ValidateUserAsync(UserCredentialDTO userCredentialDTO)
        {
            // get the user to verifty
            var userToVerify = await this.userManager.FindByNameAsync(userCredentialDTO.Username);

            if (userToVerify != null)
            {
                // check the credentials
                if (await this.userManager.CheckPasswordAsync(userToVerify, userCredentialDTO.Password))
                {
                    if (userToVerify.IsActive && !userToVerify.IsDeleted)
                    {
                        IList <string> roles = await this.userManager.GetRolesAsync(userToVerify);

                        ApplicationUserDTO modelDTO = Mapper.Map <ApplicationUser, ApplicationUserDTO>(userToVerify);
                        return(new LoggedInUserDetailsDTO()
                        {
                            ApplicationUserDTO = modelDTO,
                            Roles = roles
                        });
                    }
                    else
                    {
                        this.IsSuccess     = false;
                        this.ErrorMessages = new List <ErrorMessageDTO>()
                        {
                            new ErrorMessageDTO()
                            {
                                Message = "Your account has been deactivated. Please contact with system administrator"
                            }
                        };
                        return(null);
                    }
                }
            }
            this.IsSuccess     = false;
            this.ErrorMessages = new List <ErrorMessageDTO>()
            {
                new ErrorMessageDTO()
                {
                    Message = "Invallid username or password"
                }
            };
            return(null);
        }
        public async Task <string> Login(UserCredentialDTO credential)
        {
            var hander = new AuthHandler();
            var user   = await hander.Login(credential);

            if (user == null)
            {
                Response.StatusCode = 404;
                return("");
            }

            else
            {
                Response.StatusCode = 200;
                Session["user"]     = user;
                return("ok");
            }
        }
        //-> Login
        public async Task <UserViewDTO> Login(UserCredentialDTO crendential)
        {
            //string password = EncryptString(crendential.password);
            string password = CryptingHelper.EncryptString(crendential.password);
            var    user     = await db.tblUsers.FirstOrDefaultAsync(x => x.deleted == null && x.userName == crendential.userName && x.password == password);

            //var user = await db.tblUsers.FirstOrDefaultAsync(x => x.deleted == null && x.userName == crendential.userName);

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

            Guid token = Guid.NewGuid();

            user.session = CryptingHelper.EncryptString(token.ToString());
            await db.SaveChangesAsync();

            var userView = MappingHelper.MapDBClassToDTO <tblUser, UserViewDTO>(user);

            userView.session = token.ToString(); //***//
            return(userView);
        }
Exemple #8
0
        public async Task <object> ValidateUser([FromBody] UserCredentialDTO model)
        {
            try
            {
                var data = await _repository.ValidateUserAsync(model);

                _response.Result         = data;
                _response.IsSuccess      = _repository.IsSuccess;
                _response.ErrorMessages  = _repository.ErrorMessages;
                _response.DisplayMessage = _repository.DisplayMessage;
            }
            catch (Exception ex)
            {
                _response.IsSuccess     = false;
                _response.ErrorMessages = new List <ErrorMessageDTO>()
                {
                    new ErrorMessageDTO()
                    {
                        Message = ex.ToString()
                    }
                };
            }
            return(_response);
        }
 public IActionResult Register([FromBody] UserCredentialDTO dto)
 {
     return(this.Created(() => _UserRepo.Add(dto).Equals(TransactionStatus.SUCCESS)
             ? dto : new UserCredentialDTO(), _Logger));
 }
 public ServiceResponseDTO <CurrentUserDTO> Post([FromBody] UserCredentialDTO data)
 {
     return(_authenticationSvc.Authenticate(data.Username, data.Password));
 }